Search code examples
bashshellglobparameter-expansion

In bash, what does dir=${0%/*} means?


I have found this piece of code while studying a bash script:

dir=${0%/*}

I suspect the code inside the braces to be regex but I don't see what it means. Any idea?


Solution

  • It is not a regex, but it is a pattern match. It sets dir to the name of the script, which is $0, but without the last slash and any non-slash after it, if there is a slash in $0. If there is no slash in $0, dir gets a copy of $0 unchanged. See "Parameter Expansion" in the Bash Hackers Wiki.