if

NAME

if - execute commands if expression is true

SYNOPSIS

C shell

if (expr) command

if (expr) then
... 
else if (expr2) then
...
else
...
endif

Korn shell

if list then list [elif list then list] ... [else list] fi

DESCRIPTION

This command is a built-in command in the C shell and the Korn shell.

C shell

For the first form, if expr (an expression, as described in the tcsh(1) section on Expressions) evaluates true, command is executed. Variable substitution on command occurs early, at the same time as it does for the rest of the if command. The command argument must be a simple command, not an alias, a pipeline, a command list, or a parenthesized command list. It can have arguments. Input/output redirection occurs even if expr is false and command is thus not executed; this is a bug.

For the second form, if the specified expr is true, the commands to the first else are executed. Otherwise, if expr2 is true, the commands to the second else are executed, and so on. Any number of else-if pairs are possible; only one endif is needed. The else part is optional. The words else and endif must appear at the beginning of input lines. The if must appear alone on its input line or after an else.)

Korn shell

If the exit status of the first list is zero, the second list is executed; otherwise the list following the elif, if any, is executed with similar consequences. If all the lists following the if and each elif fail (that is, exit with non-zero status), the list following the else is executed. The exit status of an if statement is that of nonconditional list that is executed. If no nonconditional list is executed, the exit status is zero.