Bourne/bash shell scripts: string comparison

Home -> Programming -> Shell script -> Bourne shells

80938 views

From the computer of: Rex (39 recipes)
Created: Oct 21, 2003     Updated: Aug 30, 2004


3 comments:
View all comments

Add a comment

Add to:
Add to stumbleuponAdd to del.icio.usDigg itAdd to FURL

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

Subscribe to the Tech-Recipes Newsletter

You can get tips like this delivered in your email every week!

Enter your Email

We will never, ever sell your email address or spam you.




3 Recipe comments: View comments

Bourne shell scripts: string comparison by Anonymous
wrong syntax by Anonymous
comparison of 2 strings by Anonymous



Related recipes:

  Bourne/bash shell scripts: if statement syntax
  Bourne/bash shell scripts: case statement
  Bourne/bash shell script for loop syntax
  Bourne/bash shell script: while loop syntax
  Bourne/bash shell script functions
  Determine if file exists in a Bourne/bash shell script
  Determine if a file is writable by a Bourne script user
  Hide password entry in Bourne/bash shell script
  bash shell script iterate through array values
  bash shell script declaring/creating arrays

 

Sponsored links

 

Login

Nickname

Password

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.