Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ${VARIABLE+x} mean in bash? [duplicate]

Tags:

syntax

bash

I've come across code that uses this syntax in an if condition:

if [ ! -z ${VARIABLE+x} ]; then
    some commands here
fi

Does it test for an non-empty variable? If so, how is it different from ! -z "$VARIABLE"?

like image 651
corvus_192 Avatar asked Feb 22 '18 12:02

corvus_192


Video Answer


1 Answers

See PARAMETER EXPANSION in man bash:

${parameter:+word}

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

And few paragraphs above in the same section:

Omitting the colon results in a test only for a parameter that is unset.

like image 129
choroba Avatar answered Oct 18 '22 00:10

choroba