I am having trouble remembering which one of the parameter expansions ${var%subst}
or ${var#subst}
remove from the front and which one from the back of the string. Example:
$ var=/a/b/c
$ echo dirname=${var#/*} filename=${var%%*/}
dirname=a/b/c filename=/a/b/c # Wrong!
$ echo dirname=${var%/*} filename=${var##*/}
dirname=/a/b filename=c
I always mix them and either end up writing some test commands or checking the manual. It's easy to remember that %%
removes more then %
, because %%
is a longer string then %
, 2 characters vs 1 character, same for ##
vs #
. But I always mix %
with #
.
Is there a memory rule to know which %
or #
remove from which end of the string?
*.txt
file you can quickly double-click from the desktop, or perhaps define an alias tocat
the cheat sheet at the command prompt; if you end up with a lot of cheat sheets consider a shell script that runs an input arg through acase
statement to determine which cheat sheet to display – Crackbrain