でのパラメータ拡張 bash
bash
この場合、パラメータ拡張をで使用できます。
${parameter%word}
どこword
に/*
${parameter##word}
どこword
に*/
例:
最後の部分を削除
$ asdf="xxx/xxxx/xxxxx/yyy"
$ echo ${asdf%/*}
xxx/xxxx/xxxxx
これについては、man bash
以下で説明されています。
${parameter%word}
${parameter%%word}
Remove matching suffix pattern. The word is expanded to produce
a pattern just as in pathname expansion. If the pattern matches
a trailing portion of the expanded 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 pattern (the ``%%'' case) deleted. If parameter is @
or *, the pattern removal operation is applied to each posi‐
tional parameter in turn, and the expansion is the resultant
list. If parameter is an array variable subscripted with @ or
*, the pattern removal operation is applied to each member of
the array in turn, and the expansion is the resultant list.
最後の部分を除いてすべて削除
$ asdf="xxx/xxxx/xxxxx/yyy"
$ echo ${asdf##*/}
yyy
このようにスラッシュを追加できます
$ echo /${asdf##*/}
/yyy
編集された質問に従って、特定のインスタンスで必要なものを正確に取得します。しかし、その質問はその後何人かの人々によって編集されており、あなたが今何を望んでいるかを知ることは容易ではありません。
これについては、man bash
以下で説明されています。
${parameter#word}
${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. If parameter is @ or *, the
pattern removal operation is applied to each positional parame‐
ter in turn, and the expansion is the resultant list. If param‐
eter is an array variable subscripted with @ or *, the pattern
removal operation is applied to each member of the array in
turn, and the expansion is the resultant list.