What is the best way to determine if a variable in bash contains ""?

I have heard it recommended that I do if [ "x$variable" = "x" ]

Is that the correct way? (there must be something more straightforward)

link|improve this question

76% accept rate
1  
You can also ask this on stackoverflow - it's a valid programming question, and will certainly get more eyes than here... – Adam Davis May 12 '09 at 18:03
feedback

9 Answers

up vote 40 down vote accepted

This will return true if a variable is unset.

if [ -z "$VAR" ];
link|improve this answer
Does that include if a variable IS SET to a value of "" ? – Brent May 12 '09 at 18:25
Yes it does... "-z" tests for a zero-length string. – David Zaslavsky May 13 '09 at 2:40
feedback

If you're interested in distinguishing the cases of set-empty versus unset status, look at the -u option for bash:

$ set -u
$ echo $BAR
bash: BAR: unbound variable
$ [ -z "$BAR" ] && echo true
bash: BAR: unbound variable
$ BAR=""
$ echo $BAR

$ [ -z "$BAR" ] && echo true
true
link|improve this answer
feedback

-z is a the best way.

Another options I've used is to set a variable, but it can be overridden by another variable eg

export PORT=${MY_PORT:-5432}

If the $MY_PORT variable is empty, then PORT gets set to 5432, otherwise PORT is set to the value of MY_PORT. Note the syntax include the colon and dash.

link|improve this answer
Accidentally found this today, and it was exactly what I wanted. Thanks! I have to tolerate set -o nounset in some scripts. – opello Feb 16 at 23:58
feedback

An alternate I've seen to [ -z "$foo" ] is the following, however I'm not sure why people use this method, anyone know?

[ "x${foo}" = "x" ]

Anyway if you're disallowing unset variables (either by set -u or set -o nounset), then you'll run into trouble with both of those methods. There's a simple fix to this:

[ -z "${foo:-}" ]

Note: this will leave your variable undef.

link|improve this answer
There's a comment about the alternative to -z at pubs.opengroup.org/onlinepubs/009695399/utilities/test.html. Basically, it isn't meant to be an alternative to -z. Rather, it handles cases where $foo could expand to something beginning with a metacharacter that [ or test would be confused by. Putting an arbitrary non-metacharacter at the beginning eliminates that possibility. – James Sneeringer Dec 9 '11 at 19:15
feedback

the entire if-then and -z are unnecessary.

[ "$foo" ] && echo "foo is not empty"
[ "$foo" ] || echo "foo is indeed empty"
link|improve this answer
2  
That will fail if foo contains only spaces – Brian May 20 '10 at 21:16
1  
Will also fail in some shells if foo begins with a dash since it is interpreted as an option. E.g. on solaris ksh, zsh and bash are no problem, sh and /bin/test will fail – ktf Oct 20 '11 at 7:40
feedback

This is true exactly when $FOO is set and empty:

[ "${FOO+x}" = x ] && [ -z "$FOO" ]
link|improve this answer
feedback

In Bash, when you're not concerned with portability to shells that don't support it, you should always use the double-bracket syntax:

Any of the following:

if [[ -z $variable ]]
if [[ -z "$variable" ]]
if [[ ! $variable ]]
if [[ ! "$variable" ]]

In Bash, using double square brackets, the quotes aren't necessary. You can simplify the test for a variable that does contain a value to:

if [[ $variable ]]

This syntax is compatible with zsh and ksh (at least ksh93, anyway). It does not work in pure POSIX or older Bourne shells such as sh or dash.

See my answer here and BashFAQ/031 for more information about the differences between double and single square brackets.

You can test to see if a variable is specifically unset (as distinct from an empty string):

if [[ -z ${variable+x} ]]

where the "x" is arbitrary.

If you want to know whether a variable is null but not unset:

if [[ -z $variable && ${variable+x} ]]
link|improve this answer
feedback

Personally prefer more clear way to check :

if [ "${VARIABLE}" == "" ]; then
  echo VARIABLE is empty
else
  echo VARIABLE is not empty
fi
link|improve this answer
Some shells do not accept the double equal sign. – Dennis Williamson Feb 25 at 12:19
The question was about bash. I'm using bash. Works well for me. What exactly You are speaking about ? – Fedir Feb 25 at 13:03
If you're using Bash, you should use double square brackets. My previous comment was a simple statement of fact for those who might read your answer and be using a different shell. – Dennis Williamson Feb 25 at 15:49
feedback

The following is an example of how you can distinguish unset from set to the empty string, and it works in any POSIX-compatible shell:

[ -n "${VAR}" ] || echo "VAR is unset or set to the empty string"
[ -n "${VAR+set}" ] || echo "VAR is unset"
[ -z "${VAR+set}" ] || [ -n "${VAR}" ] || echo "VAR is set to the empty string"
[ -z "${VAR}" ] || echo "VAR is set to a non-empty string"

The ${VAR+foo} construct expands to the empty string if VAR is unset or to foo if VAR is set to anything (including the empty string).

The reason why [ x"${VAR}" = x ] is often recommended is because some implementations of the [ command (also known as test) are buggy. If VAR is set to something like -n, then some implementations will do the wrong thing when given [ "${VAR}" = "" ] (because the first argument to [ is erroneously interpreted as an operator, not a string).

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.