fi
Marks the end of an if statement
TLDR
View documentation for the if keyword
SYNOPSIS
The fi keyword is part of the if statement structure:
if condition; then
commands
[elif condition; then
commands]
[else
commands]
fi
It must always be preceded by an if statement and any associated then, elif, or else blocks.
PARAMETERS
No parameters
The keyword fi takes no parameters or arguments itself. It acts solely as a closing delimiter for the if statement structure.
DESCRIPTION
fi is a shell keyword, not a standalone Linux command or executable program. Its sole purpose is to terminate an if...then...fi control structure in shell scripts (such as those written for Bash, sh, zsh, or ksh). It signifies the end of the block of commands that are conditionally executed based on the if statement's condition. Without a matching fi, the shell interpreter would report a syntax error, as it would not know where the conditional block concludes. fi itself does not perform any action but provides essential structural integrity for control flow within shell scripts.
CAVEATS
fi is a shell keyword, not an executable program. It must always be paired with an opening if statement. Incorrect pairing, misspelling (e.g., 'if' without a 'fi'), or improper placement will result in shell syntax errors, preventing the script from executing correctly. It is specific to shell scripting syntax and does not apply to other programming languages.
USAGE EXAMPLE
Here is a common way fi is used within an if statement to control script flow:
#!/bin/bash
MY_VAR="hello"
if [ "$MY_VAR" = "hello" ]; then
echo "Variable is hello!"
else
echo "Variable is not hello."
fi
echo "Script continues after the if block."
In this example, fi clearly signals to the shell interpreter the end of the conditional block started by if, allowing the shell to correctly parse and execute the script.
HISTORY
The fi keyword, as an integral part of the if...then...fi control structure, was introduced with the original Bourne Shell (sh) in Unix Version 7, developed by Stephen Bourne at Bell Labs in 1977. Its design philosophy was to provide a clear, explicit closing delimiter for conditional blocks, enhancing script readability and parsing robustness. This design choice was carried over to subsequent shells like Bash, Zsh, and Ksh, making it a fundamental and ubiquitous element of Unix/Linux shell scripting. Its backwards spelling of 'if' is a common stylistic choice in shell scripting for block delimiters (e.g., case...esac, do...done).