Hubro
Hubro

Reputation: 59393

Bash test with no options appears to work, but why?

I want to pass options to my bash script. If the option "GUI" is set, zenity should be used for input and output instead of the console. Currently I'm passing the option as an environment variable like this:

GUI=1 ./my_bash_script.sh

Then I found out that I could test for the length of the variable like this:

if [ -n "$GUI" ]; then

But then, quite randomly, I discovered that just testing the string with no options also appears to work as expected:

if [ "$GUI" ]; then

I've read the manual entry for test but I can't see any explanation for what happens if you just pass a string without any arguments. I'm guessing that if it receives an empty string it returns true, and otherwise it returns false? Is that the case?

Upvotes: 2

Views: 64

Answers (1)

Carl Norum
Carl Norum

Reputation: 225072

You're correct. From the test(1) man page:

-n STRING
      the length of STRING is nonzero

STRING equivalent to -n STRING

Upvotes: 4

Related Questions