0

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?

Cyrus
  • 77,979
  • 13
  • 71
  • 125
ahg8tOPk78
  • 197
  • 1
  • 1
  • 7

1 Answers1

2

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.

cxw
  • 16,089
  • 2
  • 40
  • 75