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
  • 84,225
  • 14
  • 89
  • 153
ahg8tOPk78
  • 317
  • 1
  • 2
  • 8

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,685
  • 2
  • 45
  • 81