18

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"?

4
  • 1
    stackoverflow.com/a/13864829/214577 Commented Feb 22, 2018 at 12:07
  • @Oldskool Do you mind posting that as an answer? Commented Feb 22, 2018 at 12:11
  • I would, as that would be plagiarism :-) I'm just saying that answer pretty much answers your question. Commented Feb 22, 2018 at 12:14
  • @Oldskool You mean you wouldn't... Commented Dec 12, 2024 at 6:15

1 Answer 1

13

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.