Bourne/bash shell scripts: if statement syntax

Posted October 20, 2003 by Rex in Bourne shell scripting

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.

The Conversation

Follow the reactions below and share your own thoughts.