LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

else

Fallback branch in shell conditional statements

TLDR

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

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)

Braincup
Open source brain training for math, memory and focus
Braincup mini-games
41 mini-games · Apache-2.0
No ads · No tracking
Play in browser
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid
276 stars
From the maker of Linux Command Library
Copied to clipboard
Braincup
Open source brain training for math, memory and focus. 41 mini-games, from mental arithmetic to Sudoku, N-Back and Solo Chess.
Apache-2.0 licensed · No ads · No tracking · No account
From the maker of Linux Command Library
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid