Name

if

Synopsis

                  if 
                  test-cmds
               

Begin a conditional statement. The possible formats, shown here side by side, are:

if test-cmds    if test-cmds      if test-cmds
   then            then             then
      cmds1          
                  cmds1             
                  cmds1
fi                 else             elif test-cmds
                      cmds2            then
                fi                        cmds2
                                          ...
                                    else
                                       cmdsn
                                  fi

Usually, the initial if and any elif lines execute one test or [ ] command (although any series of commands is permitted). When if succeeds (that is, the last of its test-cmds returns 0), cmds1 are performed; otherwise, each succeeding elif or else line is tried.

Get Linux in a Nutshell, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.