Search code examples
bashparameter-expansion

bash parameter expansion and combining pattern-matching operators


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?


Solution

  • 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