1

Is it possible to use parameter expansion to combine pattern-matching operators?

For example, given the variable test=/home/archie/.vimrc.bak.

I can delete the longest match from the beginning of $test with echo ${test##*/}:

>> echo ${test##*/}
.vimrc.bak

I can also delete the shortest match from the end of $test with echo ${test%.*}:

>> echo ${test%.*}
/home/archie/.vimrc

Is there a way to combine the two pattern-matching operators to output .vimrc?

4

1 回答 1

3

You cannot do it in a single expansion, but you can do it with two:

$ test=/home/archie/.vimrc.bak; tmp="${test%.*}"; name="${tmp##*/}"; echo "$name"
.vimrc
于 2017-10-08T02:28:22.420 回答