Bourne/bash shell scripts: if statement syntax

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

78313 views

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


Add a comment

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

Conditional expressions give programs life. The ability to branch makes shell scripts powerful. This recipe shows the basic if then else structure for sh, ksh, bash, zsh, etc.

if [ condition_A ]
then
   code to run if condition_A true
elif [ condition_B ]
then
   code to run if condition_A false and condition_B true
else
   code to run if both conditions false
fi


Multiple elif blocks can be strung together to make an elaborate set of conditional responses.

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.





Related recipes:

  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
  Bourne/bash shell scripts: string comparison
  bash shell script accessing array variables
  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.