elif
Chain conditional statements in scripts
TLDR
View documentation for if command
SYNOPSIS
if condition1; then
commands_if_true
elif condition2; then
commands_elif_true
elif condition3; then
commands_elif_true_2
[ else
commands_else_true ]
fi
PARAMETERS
condition
A shell command or expression that returns an exit status. An exit status of 0 (success) is considered true, and any non-zero exit status (failure) is considered false. Common conditions involve test (or [ ]), [[ ]], or any command whose exit status needs to be evaluated.
DESCRIPTION
The elif construct, short for "else if," is not a standalone Linux command but a fundamental keyword used within conditional statements in shell scripting (e.g., Bash, sh, ksh). It allows for chaining multiple conditions, providing an alternative path of execution if the preceding if or elif conditions evaluate to false. When an if statement is encountered, its condition is tested. If true, the commands following its then keyword are executed, and the entire if block is exited. If the if condition is false, the shell then checks the first elif condition. This process continues: if an elif condition is true, its associated commands are executed, and the block terminates. If all if and elif conditions are false, the commands within the optional else block are executed, if present. The entire conditional structure must be terminated by fi. This mechanism is crucial for creating scripts that can make decisions based on multiple, sequential tests.
CAVEATS
elif is a keyword, not a standalone executable command. It must be used within an if...fi block.
Each elif block requires a preceding if or another elif and must be followed by then and the condition to be tested.
Conditions are evaluated sequentially; only the first true condition's associated commands are executed.
EXAMPLE USAGE
A practical example demonstrating the use of elif:
#!/bin/bash
read -p "Enter a number: " num
if (( num > 10 )); then
echo "Number is greater than 10."
elif (( num == 10 )); then
echo "Number is exactly 10."
else
echo "Number is less than 10."
fi
In this example, if 'num' is not greater than 10, the elif condition checks if it's equal to 10. Only if both previous conditions are false, the else block is executed.
COMPARISON WITH 'CASE' STATEMENT
While elif is suitable for sequential tests of various conditions, the case statement (e.g., case variable in pattern) is often preferred for multiple conditional branches based on the value of a single variable matching different patterns. For complex pattern matching or a long list of specific values for one variable, case can be more readable and efficient than a long chain of elif statements. However, elif offers greater flexibility for evaluating completely independent or complex conditions sequentially.
HISTORY
The concept of chained conditional statements, including elif (or similar constructs), has been a fundamental part of Unix shell programming since the early days of the Bourne shell (sh) in the late 1970s. It provides a standard and widely adopted method for implementing complex decision-making logic in scripts, and its syntax has been consistently carried over into modern shells like Bash (bash), Ksh (ksh), and Zsh (zsh), making it a cornerstone of shell scripting.