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 ]
code to run if condition_A true
elif [ condition_B ]
code to run if condition_A false and condition_B true
code to run if both conditions false

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.