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.
Add a comment
|
1 Answer
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$VAR
if possible, elsevalue
${VAR:=value}
Use$VAR
if possible, else set$VAR
tovalue
and usevalue
${VAR:?value}
(exit if$VAR
is 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.)