I'm quite confused about the following regular expressions I found in a shell script:
${0##*/}
${0%/*}
How do they work?
I'm quite confused about the following regular expressions I found in a shell script:
${0##*/}
${0%/*}
How do they work?
Those are not regular expressions, they are examples of Bash's parameter expansion: the substitution of a variable or a special parameter by its value. The Wooledge Wiki has a good explanation.
Basically, in the example you have, ${0##*/} translates as:
for the variable $0, and the pattern '/', the two hashes mean from the beginning of the parameter, delete the longest (or greedy) match—up to and including the pattern.
So, where $0 is the name of a file, eg., $HOME/documents/doc.txt, then the parameter would be expanded as: doc.txt
Similarly, for ${0%/*}, the pattern / is matched against the end of parameter (the %), with the shortest or non-greedy match deleted – which in the example above would give you $HOME/documents.
See also the article on the Bash Hacker's Wiki.