Suzan Cioc
Suzan Cioc

Reputation: 30097

What does "plus colon" ("+:") mean in shell script expressions?

What does this mean?

if ${ac_cv_lib_lept_pixCreate+:} false; then :
  $as_echo_n "(cached) " >&6
else
  ac_check_lib_save_LIBS=$LIBS

Looks like ac_cv_lib_lept_pixCreate is some variable, so what does +: mean?

Where to learn complete syntax of curly bracket expressions? What is the name of this syntax?

Upvotes: 24

Views: 17067

Answers (4)

Henk Langeveld
Henk Langeveld

Reputation: 8446

In the “plus colon” ${...+:} expression, only the + has special meaning in the shell. The colon is just a string value in this case, so we could write that snippet as ${...+":"}. But, because it is also the first word in a shell command list, it becomes the command : which always returns true.

Depending on the question if the variable has a value or not, the if statement becomes either if true false; or if false; .


Let's break it down:

For convenience, let's pretend the variable is called var, and consider the expression:

if ${var+:} false; then ...

If the shell variable $var exists, the entire expression is replaced with :, if not, it returns an empty string.

Therefore the entire expression ${var+:} false becomes either : false (returning true) or false (returning false).

This comes down to a test for existence, which can be true even if the variable has no value assigned.

It is very cryptic, but as it happens, is one of the few tests for the existence of a variable that actually works in most, if not all, shells of Bourne descent.

Possible equivalents: (substitute any variable name here for var)

if [[ ${var+"is_set"} == is_set ]]; then ...

Or, probably more portable:

case ${var+"IS_SET"} in IS_SET) ...;; esac

Upvotes: 31

Sir Athos
Sir Athos

Reputation: 9867

To illustrate what has already been said:

Unset variable (note the blank lines as a result of some echo commands):

$ unset foo
$ echo ${foo}

$ echo ${foo:+:}

$ echo ${foo+:}

Null variable:

$ foo=""
$ echo ${foo}

$ echo ${foo:+:}

$ echo ${foo+:}
:

Non-null variable:

$ foo="bar"
$ echo ${foo}
bar
$ echo ${foo:+:}
:
$ echo ${foo+:}
:

Upvotes: 10

Srini V
Srini V

Reputation: 11355

Simple examples will prove

I check for presence of a parameter TEST, if present echo "Yes" else I echo "No"

openvas:~$ ${TEST+:} false  &&  echo "yes" || echo "no"
no
openvas:~$ TEST=1
openvas:~$ ${TEST+:} false  &&  echo "yes" || echo "no"
yes
openvas:~$ 

If you see, the parameter TEST is evaluated and it is actually unset, so it returns false and exit the path and goes to the OR Once I set the same, and test again, it flows to the echo part (continued &&) since it returns true

Refer: this and that

Upvotes: 3

nickgrim
nickgrim

Reputation: 5437

Shell Parameter Expansion documentation for bash is here. No mention of +:, though it does mention :+:

${parameter:+word}
If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

Upvotes: 21

Related Questions