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 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.