The if statement

The if statement uses the exit status of the given command and conditionally executes the statements following. The general syntax is:

   if test
   then
      commands     (if condition is true)
   else
      commands     (if condition is false)
   fi

then, else and fi are shell reserved words and as such are only recognised after a newline or ; (semicolon). Make sure that you end each if construct with a fi statement.

if statements may be nested:

   if ...
   then ...
   else if ...
       ...
     fi
   fi

The elif statement can be used as shorthand for an else if statement. For example:

   if ...
   then ...
   elif ...
     ...
   fi

[Home] [Search] [Index] This site maintained by webmaster@deslab.mit.edu