Conditional constructs – if else

We use the if command to check the pattern or command status and accordingly we can make certain decisions to execute scripts or commands.

The syntax of the if conditional is as follows:

    if     command
    then
        command
        command
    fi

From the preceding syntax, we can clearly understand the working of the if conditional construct. Initially, if will execute the command. If the result of command execution is true or 0, then all the commands that are enclosed between then and fi will be executed. If the status of command execution after if is false or nonzero, then all the commands after then will be ignored and the control of execution will directly go to fi.

Let's learn another variation of if constructs.

Syntax:

 if command then ...

Get Learning Linux Shell Scripting 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.