views:

63

answers:

4

how to find variable is empty in shell script

A: 
if [[ "$variable" == "" ]] ...
Amardeep
+1  A: 

Presuming bash:

var=""
if [ -n "$var" ]; then
    echo "not empty"
else
    echo "empty"
fi
ChristopheD
glenn jackman
@glenn jackman: good comment; it's true that `-z` is closer to what was asked. Jay has put this in his answer so I'll refrain from updating mine and leave this up as is.
ChristopheD
+2  A: 

In bash at least: if [[ -z "$var" ]]

the command line "man test" is your friend.

Jay
A: 
[ "$variable" ] || echo empty
: ${variable="value_to_set_if_unset"}
pixelbeat