LinuxCommandLibrary

fi

Marks the end of an if statement

TLDR

View documentation for the if keyword

$ tldr if
copy

SYNOPSIS

fi

DESCRIPTION

The `fi` command in Linux marks the end of an `if`, `elif`, or `else` conditional block in shell scripts. It acts as a closing statement, signaling to the shell interpreter that the preceding conditional logic has concluded. Without a corresponding `fi`, the shell will typically report a syntax error. While `fi` itself doesn't execute any actions, it's a crucial element for structuring conditional execution flows within scripts, ensuring correct parsing and behavior. Think of it as the closing parenthesis in an equation or the closing curly brace in many programming languages, defining the scope and boundaries of the conditional construct.

CAVEATS

The `fi` command must be used to properly terminate an `if`, `elif`, or `else` block. Failure to do so will result in a syntax error and script execution failure.

EXAMPLE USAGE

Example:
if [ "$VAR" = "value" ]; then
echo "VAR is value";
fi


This simple `if` statement checks if the variable VAR is equal to "value". The `fi` marks the end of the block.

NESTING

Conditional blocks can be nested. Each `if` block must have its own closing `fi`.

Example:
if [ "$A" = "1" ]; then
if [ "$B" = "2" ]; then
echo "A is 1 and B is 2";
fi
fi


In this example, there are two nested `if` statements. Each requires its own `fi`.

HISTORY

The `fi` command is fundamental to shell scripting and has been present since the early versions of Unix shells, including the Bourne shell, which is the ancestor of many modern shells like bash. Its purpose has remained consistent: to clearly delimit conditional blocks within shell scripts, enabling more complex and dynamic program flow. It is derived from ALGOL68.

SEE ALSO

if(1), else(1), elif(1)

Copied to clipboard