はじめに

よく使う変数内文字列置換を忘れるのでのメモ。

~/tmp/hoge.sh

#!/bin/bash

echo "$0"

# basename
echo "${0##*/}"

# 拡張子取得
echo "${0##*\.}"

# dirname
echo "${0%/*}"

# 拡張子以外
echo "${0%.*}"

output

/Users/oppara/tmp/hoge.sh
hoge.sh
sh
/Users/oppara/tmp
/Users/oppara/tmp/hoge

環境

% sw_vers
ProductName:    macOS
ProductVersion: 12.2
BuildVersion:   21D49

% bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin21)
Copyright (C) 2007 Free Software Foundation, Inc.