Bash 変数 ${0##*/}

Bash 変数 ${0##*/}

${0##*/}Bashスクリプトで出会う変数を理解しようとしています。

$0スクリプトの名前またはパスが含まれていることを知ってから、[次へ]を##クリックします${parameter##pattern}源泉)。

/しかし、私は彼らがここで何をしているのか理解していません。私は2つのスラッシュを持つこの構文だけを知っています。${parameter/pat/string}

Bashでこの変数をエコーすると、次のような結果が得られますbash。 :)

最後に、スクリプトを共有する権限がありません。変数がステートメントSOFT="${0##*/}"で呼び出され使用されると言いたいと思います。printf"Error message sent by $SOFT"

ベストアンサー1

これにより、以前のすべてのパス要素がそのまま切り捨てられますbasename $0##プレフィックスパターンに一致する最も長い拡張子を見つけます。

$ x=/a/b/c/d
$ echo ${x##*/}
d
$ basename $x
d

マニュアルページから:

${parameter##word}
       Remove matching prefix pattern.  The word is expanded to produce
       a pattern just as in pathname expansion.  If the pattern matches
       the  beginning of the value of parameter, then the result of the
       expansion is the expanded value of parameter with  the  shortest
       matching  pattern  (the ``#'' case) or the longest matching pat‐
       tern (the ``##'' case) deleted.  

これを使用する理由${0##*/}は、外部プログラム呼び出しを含まないが、何が起こっているのか曖昧にするからです。

おすすめ記事