StaticMethod
StaticMethod

Reputation: 837

In bash what does if [ "$VAR" ] mean

I understand how the if function works in bash, but the problem is that I don't get what it means if you aren't comparing it with something. I know there are a bunch of switches like -e or -c or -f, but when would the following code get evaluated as true?

if [ "$VAR" ]; then
  echo "TRUE"
else
  echo "FALSE"
fi

I'm trying to interpret a script someone wrote for me.

Upvotes: 1

Views: 1013

Answers (2)

Anton Kovalenko
Anton Kovalenko

Reputation: 21507

I think it's the following:

-n STRING
       the length of STRING is nonzero
STRING equivalent to -n STRING

Just the test for non-empty value.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798516

From help test:

     STRING      True if string is not empty.

Upvotes: 4

Related Questions