You need to do the parsing in stages:-
path=/dir0/dir1/dir2/filename.ext
dir=${path%/*}
lastdir=${dir##*/}
By attempting to combine both parsing operations, you were attempting to use a string instead of a variable in the second expansion: this is not supported.
Note that you can use the same variable name for both dir
and lastdir
, eg:-
cut=${path%/*}; cut=${cut##*/}
You haven't said that your shell is bash
, but I have assumed this from your code sample.