You could use that with a flag variable to e.g. add some flag to a command line:
use_x=1
param_x=foobar
somecmd ${use_x:+-x} ${use_x:+$param_x}
Of course, the part with param_x
isn't such a good idea, with it being subject to word-splitting and globbing. That shouldn't be a problem for the static flag itself, though, but in general, using an array here would be more robust.
To test if the variable is set, [ -n "$var" ]
works similarly, so there's not much use for ${var:+value}
. On the other hand, ${var+value}
(without the colon) is useful to tell the difference between an empty and an unset variable:
unset a
b=
[ "${a+x}" = "x" ] && echo a is set
[ "${b+x}" = "x" ] && echo b is set
${variable:+"$variable"}
is a useful way of quoting$variable
without getting an empty string if it happens to be empty. – cas Jul 25 '17 at 02:04