Conditional Statements

Conditional statements are used in shell programs to decide which part of the program to execute depending on specified conditions.

The if Statement

The if statement evaluates a logical expression to make a decision. An if condition has the following format in pdksh and bash:

if [ expression ]; then
    Statements
elif [expression ]; then
    Statements
else
    Statements
fi

The if conditions can be nested. That is, an if condition can contain another if condition within it. It is not necessary for an if condition to have an elif or else part. The else part is executed if none of the expressions that are specified in the if statement and are optional in subsequent elif statements are true. The word fi is used to indicate the ...

Get Red Hat® Linux® 7 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.