Bourne/bash shell scripts: string comparison

Contributor Icon Contributed by Rex Date Icon October 21, 2003  
Tag Icon Tagged: Bourne shell scripting


To determine if the value of a variable ($var) is empty (null):

if [ $var == "" ]
then
echo variable is null
fi

To determine if the value of a variable is not empty:

if [ $var != "" ]
then
echo variable is not null
fi

To compare the contents of a variable to a fixed string:

if [ $var == "value" ]
then
echo is the same
fi

To determine if variable’s contents are not equal to a fixed string:

if [ $var != "value" ]
then
echo not the same
fi

Previous recipe | Next recipe |
 

Viewing 4 Comments

 
close Reblog this comment
blog comments powered by Disqus