LinuxCommandLibrary

else

Execute code when 'if' condition is false

TLDR

View documentation for the if keyword

$ tldr if
copy

SYNOPSIS

The else keyword's 'synopsis' is best understood in the context of the conditional constructs it is part of, as it does not take standalone options or arguments.

Commonly in 'if' statements:
if condition;
then
    commands_if_true
else
    commands_if_false
fi


In this structure, else marks the beginning of the command block that runs when condition evaluates to false.

DESCRIPTION

The else keyword in Linux is not a standalone executable command but a fundamental control flow construct used within shell scripting languages like Bash, Zsh, and others. It forms an integral part of conditional statements, most notably the if...then...else...fi structure. Its primary purpose is to define an alternative block of commands to be executed only when the condition specified in the preceding if statement evaluates to false (non-zero exit status). If the if condition is true, the commands following then are executed, and the else block is skipped. While less common, else can also conceptually represent the default or fallback path in case statements, though typically the * pattern is used for this purpose to catch all non-matching cases.

CAVEATS

The else keyword is not an executable Linux command found in the file system (e.g., /bin/else). Instead, it is a built-in keyword of various Unix-like shells (e.g., Bash, Zsh, Ksh). This means it does not have its own manual page (man else) nor does it accept command-line options or arguments. Its functionality is entirely dependent on its use within specific shell scripting control structures, primarily if...then...fi blocks. Misplacing else or using it outside of a valid conditional context will result in a shell syntax error.

USAGE IN `IF` STATEMENTS

The most common application of else is within the if...then...else...fi construct. It provides an alternative execution path when the if condition evaluates to false. If the if condition is true, the else block is entirely skipped. An if statement can exist without an else branch, in which case nothing happens if the condition is false.

Example:
if [ -f "myfile.txt" ]; then
    echo "myfile.txt exists."
else
    echo "myfile.txt does not exist."
fi

DISTINCTION FROM `CASE` STATEMENT DEFAULTS

While else conceptually provides a 'default' path, it is rarely used directly within case statements in the same way as if statements. In case statements, the common practice for defining a default or fallback action (when none of the specified patterns match) is to use the wildcard pattern *. This pattern matches any string, effectively serving the role of an 'else' block for case statements.

Example using wildcard for default:
case "$fruit" in
    apple) echo "It's an apple." ;;
    banana) echo "It's a banana." ;;
    *) echo "Unknown fruit." ;;
esac

SHELL KEYWORD VS. COMMAND

It's crucial to understand that else is a reserved keyword for the shell's parser, not an external command. This means it doesn't have a separate executable file, nor can it be invoked directly like /bin/ls. Its interpretation and function are handled internally by the shell itself as part of its control flow syntax.

HISTORY

The concept of an else branch in conditional logic is fundamental to programming paradigms and has been present since the early days of computing. In the context of Unix-like operating systems, the else keyword was an integral part of the original Bourne shell (sh), developed by Stephen Bourne at Bell Labs in the late 1970s. Its inclusion provided essential conditional control flow capabilities for scripting, allowing for more complex and robust automation. Subsequent shells, such as the Korn shell (ksh), Bash (bash), and Zsh (zsh), inherited and maintained this core functionality, making else a universal and indispensable keyword in Unix shell scripting environments.

SEE ALSO

bash(1), sh(1), ksh(1), zsh(1), test(1), [(1)

Copied to clipboard