I've run a cross a shell script with BUILDDIR=${BUILDDIR:-"/data"} which , upon experimentation, takes the original BUILDDIR if it exists and isnt an empty string, and otherwise sets it to /data.
What I don't understand is how the expression works - why the : and - operators and how they work.
Asked
Active
Viewed 23 times
0
Rui F Ribeiro
- 56,709
- 26
- 150
- 232
jeremy_rutman
- 240
1 Answers
1
It's one of (fortunately only) a handful of shortcuts done as part of parameter expansion. In short, there's the following, loosely defined:
${VAR:-value}Use$VARif possible, elsevalue${VAR:=value}Use$VARif possible, else set$VARtovalueand usevalue${VAR:?value}(exit if$VARis undefined) and${VAR:+value}(opposite of:-) exist, but I've never seen them in the wild.${VAR:offset}and${VAR:offset:length}take substrings of$VAR.
(There's also a bunch of others that remove prefixes or suffixes or do general substitution; please see the bash info page linked above for those.)
Ulrich Schwarz
- 15,989