elif
Conditional else-if construct in shell scripts
TLDR
SYNOPSIS
if condition; then commands; elif condition; then commands; fi
DESCRIPTION
elif is a shell keyword used in bash and other POSIX shells for conditional branching. It's short for "else if" and allows multiple conditions to be tested sequentially.elif follows an if statement and precedes then. Multiple elif blocks can appear before an optional else. Only the first matching condition's block executes.This construct is fundamental to shell scripting for implementing multi-way branching logic.
CAVEATS
elif is a shell reserved word, not a program, so `which elif` finds nothing and it cannot be used outside an if block. Each elif needs its own then, and the whole chain still ends with a single fi. Inside `[ ... ]` every token must be separated by spaces: `[$x -eq 1]` is a syntax error. The condition is a command list, so it branches on the exit status, and any command works there, not just test.
HISTORY
elif is part of POSIX shell syntax, inherited from the Bourne shell created by Stephen Bourne at Bell Labs in the 1970s. It provides cleaner syntax than nested if-else blocks.
