else

Fallback branch in shell conditional statements

TLDR

Use else in bash script
$ if [condition]; then echo "true"; else echo "false"; fi
Else after elif
$ if [ $x -lt 0 ]; then echo "negative"; elif [ $x -eq 0 ]; then echo "zero"; else echo "positive"; fi
Fall back when a file is missing
$ if [ -f [config.yml] ]; then echo "using config"; else echo "using defaults"; fi

SYNOPSIS

if condition; then commands; else commands; fi

DESCRIPTION

else is a shell keyword providing an alternative branch in conditional statements. It executes when the preceding if (and any elif) conditions are false.else appears after if/elif blocks and before fi. It cannot have a condition - it catches all remaining cases. Only one else block is allowed per if statement.This is a fundamental shell scripting construct for handling the "otherwise" case in conditional logic.

CAVEATS

else is a shell reserved word, not a program, so it cannot be used outside an if block. Unlike if and elif it takes no condition and no then, and only one else may appear per if statement, always as the last branch before fi. A `case` statement has no else: its catch-all is the `*)` pattern.

HISTORY

else is part of POSIX shell syntax from the Bourne shell, created by Stephen Bourne at Bell Labs. It provides the standard fallback mechanism in shell conditional statements.

SEE ALSO

if(1), elif(1), fi(1), test(1), bash(1)