1

From Bash manual

${parameter:+word}

If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

It looks like string replacement only when the target string in "parameter" exists and is not null.

What are the purpose and use cases of this kind of parameter expansion?

Thanks.

closed as too broad by Jeff Schaller, don_crissti, Rui F Ribeiro, GAD3R, Romeo Ninov Jul 25 '17 at 6:01

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.