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.


1 Answer 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 $VAR if possible, else value
  • ${VAR:=value} Use $VAR if possible, else set $VAR to value and use value
  • ${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.)

Not the answer you're looking for? Browse other questions tagged .