Bash If Expressions

Conditional expressions are used to control the flow of execution based on certain conditions. They allow you to make decisions and execute different parts of the script depending on whether conditions are true or false.

In Bash scripts, conditional expressions include if, elif (else if), and else statements. These allow our scripts to handle different situations. The basic syntax for an if statement in Bash is as follows:

if [ condition ]; then
    # commands to execute if condition is true
fi

In this example, ‘condition‘ is an expression that evaluates to true or false. If the condition is true, the commands within the if block are executed.

Note that if expressions in Bash require usage of the ‘then‘ keyword, as shown above. This isn’t required in most modern programming languages, so we are bringing special attention to it. Additionally, the conditional statement to be evaluated lives in square brackets, while in most other languages if expressions use parentheses. Finally, the if expression is closed with ‘fi‘.

Conditional expressions are powerful, and in Bash they have some special properties that are helpful to understand and play with. In this article, we’ll cover the basic if statement in detail, as well as conditionals that cover more complex situations, like if…else and if…elif…else statements. We’ll also see examples for using conditional expressions in a variety of situations that are common in Bash scripts.

If Statements in Bash

Here’s a simple example of an if statement in a Bash script that doesn’t take user input:

#!/bin/bash

# Simple if statement
value=10

if [ $value -eq 10 ]; then
    echo "The value is 10"
fi

In this example, the script checks if the variable value is equal to 10 using the -eq (equal) operator. If the condition is true, it prints “The value is 10” to the console.

if…else Statements in Bash

You can also include an else block to specify commands to execute if the condition is false:

if [ condition ]; then
    # commands to execute if condition is true
else
    # commands to execute if condition is false
fi

Additionally, you can use elif to specify additional conditions to check:

if [ condition1 ]; then
    # commands to execute if condition1 is true
elif [ condition2 ]; then
    # commands to execute if condition2 is true
else
    # commands to execute if neither condition1 nor condition2 is true
fi

In Bash, conditional expressions can include various tests such as string comparisons, numerical comparisons, file tests, and logical operations. Here are some common examples:

Bash If Statement Example

Here’s a simple example of an if statement in a Bash script:

#!/bin/bash

# Simple if statement
value=10

if [ $value -eq 10 ]; then
    echo "The value is 10"
fi

In this example, the script checks if the variable value is equal to 10 using the -eq (equal) operator. If the condition is true, it prints “The value is 10” to the console.

Using If Statements With User Input in Bash

One of the most common use cases for if statements in Bash is flow control based on user input. In other words, we want the flow of code execution to be based partly on inputs or choices made by the user.

This is extremely common, as by nature the script will execute using Bash, the most popular shell in Unix-like environments.

Here’s a simple example of an if statement in a Bash script that takes user input and echoes a different string to the terminal depending on the evaluation of the input:

#!/bin/bash

# Check if a number is positive, negative, or zero

echo "Enter a number:"
read num

if [ $num -gt 0 ]; then
    echo "$num is positive"
elif [ $num -lt 0 ]; then
    echo "$num is negative"
else
    echo "$num is zero"
fi

In this script, the user is prompted to enter a number. The script then uses an if statement to check if the number is greater than (-gt), less than (-lt), or equal to (-eq) zero, and prints an appropriate message based on the result.

Check out our tutorial on operators in Bash if the -gt, -lt or -eq comparison operators are unfamiliar.

Working With If Statements in Bash

There are unlimited ways of working with if statements to control the flow of execution in a Bash script.

The following examples demonstrate how to use if statements for string and numerical comparisons, file tests, and logical operations:

String Comparisons

if [ "$var" == "value" ]; then
    # commands
fi

Numerical Comparisons

if [ "$num1" -eq "$num2" ]; then
    # commands
fi

File Tests

if [ -f "$file" ]; then
    # commands
fi

Logical Operations

if [ "$var" == "value" ] && [ "$num" -gt 10 ]; then
    # commands
fi

Conditional expressions in Bash provide a powerful way to add decision-making capabilities to your scripts, allowing them to adapt to different scenarios and conditions.