The documentation is a bit painful to read, so I've summarised it in a simpler way.
Note that the '*
' needs to swap places with the '
' depending on whether you use #
or %
. (The *
is just a wildcard, so you may need to take off your "regex hat" while reading.)
${A% *}
- remove shortest trailing*
(strip the last word)${A%% *}
- remove longest trailing*
(strip the last words)${A#* }
- remove shortest leading*
(strip the first word)${A##* }
- remove longest leading*
(strip the first words)
Of course a "word" here may contain any character that isn't a literal space.
You might commonly use this syntax with other characters to trim filenames:
${A##*/}
removes all containing folders, if any, from the start of the path, e.g.
/usr/bin/git
->git
/usr/bin/
-> (empty string)
/usr/bin
->bin
${A%/*}
removes the last file/folder/trailing slash, if any, from the end:
/usr/bin/git
->/usr/bin
/usr/bin/
->/usr/bin
/usr/bin
->/usr/
${A%.*}
removes the last extension, if any (just be wary of things like/my.path/noext
):
archive.tar.gz
->archive.tar
/my.path/noext
->/my
(!)
To avoid the last issue, you use an explicit extension if you know it: %.ext
instead of %.*