What is the meaning of the ${0##…} syntax with variable, braces and hash character in bash?

See the section on Substring removal in the Advanced Bash-Scripting Guide‡:

${string#substring}

Deletes shortest match of substring from front of $string.

${string##substring}

Deletes longest match of substring from front of $string.

The substring may include a wildcard *, matching everything. The expression ${0##/*} prints the value of $0 unless it starts with a forward slash, in which case it prints nothing.

‡ The guide, as of 3/7/2019, mistakenly claims that the match is of $substring, as if substring was the name of a variable. It’s not: substring is just a pattern.

Leave a Comment