Bash if...else Statement

When writing shell scripts, you will often need to run different commands depending on whether a condition is true or false. Bash provides the if, if...else, and if...elif...else statements to handle exactly that, letting you branch your script logic based on test results, exit codes, or file checks.
This guide walks through each form of the Bash if statement with practical examples.
if Statement
Bash if conditionals can have different forms. The most basic if statement takes the following form:
if TEST-COMMAND
then
STATEMENTS
fiThe if statement starts with the if keyword followed by the conditional expression and the then keyword. The statement ends with the fi keyword.
If the TEST-COMMAND evaluates to True, the STATEMENTS are executed. If the TEST-COMMAND returns False, nothing happens; the STATEMENTS are ignored.
As a general practice, indent your code and separate code blocks with blank lines. Most people use either 4-space or 2-space indentation. Indentations and blank lines make your code more readable and organized.
The following example script checks whether a given number is greater than 10:
#!/bin/bash
echo -n "Enter a number: "
read VAR
if [[ $VAR -gt 10 ]]
then
echo "The variable is greater than 10."
fiSave the code in a file and run it from the command line:
bash test.shThe script will prompt you to enter a number. If, for example, you enter 15, the test command will be evaluated as true because 15 is greater than 10, and the echo
command inside the then clause will be executed.
The variable is greater than 10.if...else Statement
The Bash if...else statement takes the following form:
if TEST-COMMAND
then
STATEMENTS1
else
STATEMENTS2
fiIf the TEST-COMMAND evaluates to True, the STATEMENTS1 will be executed. Otherwise, if TEST-COMMAND returns False, the STATEMENTS2 will be executed. You can have only one else clause in the statement.
Adding an else clause to the previous script gives a message for both outcomes:
#!/bin/bash
echo -n "Enter a number: "
read VAR
if [[ $VAR -gt 10 ]]
then
echo "The variable is greater than 10."
else
echo "The variable is less than or equal to 10."
fiIf you run the code and enter a number, the script will print a different message based on whether the number is greater or less/equal to 10.
if...elif...else Statement
The Bash if...elif...else statement takes the following form:
if TEST-COMMAND1
then
STATEMENTS1
elif TEST-COMMAND2
then
STATEMENTS2
else
STATEMENTS3
fiIf the TEST-COMMAND1 evaluates to True, the STATEMENTS1 will be executed. If the TEST-COMMAND2 evaluates to True, the STATEMENTS2 will be executed. If none of the test commands evaluate to True, the STATEMENTS3 will be executed.
You can have one or more elif clauses in the statement. The else clause is optional.
The conditions are evaluated sequentially. Once a condition returns True, the remaining conditions are not tested, and program control moves to the end of the if statements.
Adding an elif clause handles the equal case as well:
#!/bin/bash
echo -n "Enter a number: "
read VAR
if [[ $VAR -gt 10 ]]
then
echo "The variable is greater than 10."
elif [[ $VAR -eq 10 ]]
then
echo "The variable is equal to 10."
else
echo "The variable is less than 10."
fiNested if Statements
Bash allows you to nest if statements within if statements.
You can place multiple if statements inside another if statement.
The following script will prompt you to enter three numbers and print the largest number among the three numbers.
#!/bin/bash
echo -n "Enter the first number: "
read VAR1
echo -n "Enter the second number: "
read VAR2
echo -n "Enter the third number: "
read VAR3
if [[ $VAR1 -ge $VAR2 ]]
then
if [[ $VAR1 -ge $VAR3 ]]
then
echo "$VAR1 is the largest number."
else
echo "$VAR3 is the largest number."
fi
else
if [[ $VAR2 -ge $VAR3 ]]
then
echo "$VAR2 is the largest number."
else
echo "$VAR3 is the largest number."
fi
fiHere is what the output will look like:
Enter the first number: 4
Enter the second number: 7
Enter the third number: 2
7 is the largest number.case statement
is often a cleaner alternative.Multiple Conditions
The logical OR and AND operators allow you to use multiple conditions in the if statements.
Here is another version of the script to print the largest number among the three numbers. In this version, instead of the nested if statements, we are using the logical AND (&&) operator.
#!/bin/bash
echo -n "Enter the first number: "
read VAR1
echo -n "Enter the second number: "
read VAR2
echo -n "Enter the third number: "
read VAR3
if [[ $VAR1 -ge $VAR2 ]] && [[ $VAR1 -ge $VAR3 ]]
then
echo "$VAR1 is the largest number."
elif [[ $VAR2 -ge $VAR1 ]] && [[ $VAR2 -ge $VAR3 ]]
then
echo "$VAR2 is the largest number."
else
echo "$VAR3 is the largest number."
fiTest Operators
In Bash, the test command takes one of the following syntax forms. For a complete guide on numeric comparisons, see our Bash comparison operators
tutorial.
test EXPRESSION
[ EXPRESSION ]
[[ EXPRESSION ]]To make the script portable, prefer using the old test [ command, which is available on all POSIX shells. The newer version of the test command [[ (double brackets) is supported on most modern systems using Bash, Zsh, and Ksh as default shells.
To negate the test expression, use the logical NOT (!) operator. When comparing strings
, always use single or double quotes to avoid word splitting and globbing issues.
Below are some of the most commonly used operators:
-nVAR- True if the length ofVARis greater than zero.-zVAR- True if theVARis empty.STRING1 = STRING2- True ifSTRING1andSTRING2are equal.STRING1 != STRING2- True ifSTRING1andSTRING2are not equal.INTEGER1 -eq INTEGER2- True ifINTEGER1andINTEGER2are equal.INTEGER1 -gt INTEGER2- True ifINTEGER1is greater thanINTEGER2.INTEGER1 -lt INTEGER2- True ifINTEGER1is less thanINTEGER2.INTEGER1 -ge INTEGER2- True ifINTEGER1is equal or greater than INTEGER2.INTEGER1 -le INTEGER2- True ifINTEGER1is equal or less thanINTEGER2.-hFILE- True if theFILEexists and is a symbolic link.-rFILE- True if theFILEexists and is readable.-wFILE- True if theFILEexists and is writable.-xFILE- True if theFILEexists and is executable.-dFILE- True if theFILEexists and is a directory.-eFILE- True if theFILEexists and is a file, regardless of type (node, directory, socket, etc.).-fFILE- True if theFILEexists and is a regular file (not a directory or device).
Here are a few practical examples using common operators.
Check whether two strings are equal:
#!/bin/bash
read -p "Enter your username: " USER
if [[ "$USER" = "admin" ]]
then
echo "Welcome, admin."
else
echo "Access denied."
fiCheck whether a variable is empty:
#!/bin/bash
read -p "Enter a value: " INPUT
if [[ -z "$INPUT" ]]
then
echo "No value provided."
fiCheck whether a file exists before reading it:
#!/bin/bash
FILE="/etc/hosts"
if [[ -f "$FILE" ]]
then
echo "$FILE exists."
else
echo "$FILE not found."
fiOne-Line Syntax
You can write if statements on a single line, which is useful for simple conditions:
if [[ $VAR -gt 10 ]]; then echo "Greater than 10"; fiWith if...else:
if [[ $VAR -gt 10 ]]; then echo "Greater"; else echo "Smaller or equal"; fiShort-Circuit Evaluation
For simple conditions, you can use the logical AND (&&) and OR (||) operators as a shorthand:
[[ $VAR -gt 10 ]] && echo "Greater than 10"This is equivalent to:
if [[ $VAR -gt 10 ]]; then
echo "Greater than 10"
fiYou can also combine both operators for an if...else effect:
[[ $VAR -gt 10 ]] && echo "Greater" || echo "Smaller or equal"&& and || combinations. If the command after && fails, the command after || will also execute.Quick Reference
| Syntax | Description |
|---|---|
if [[ condition ]]; then ... fi | Basic if statement |
if [[ condition ]]; then ... else ... fi | If with else |
if [[ c1 ]]; then ... elif [[ c2 ]]; then ... else ... fi | If with elif |
[[ condition ]] && command | Execute if true |
[[ condition ]] || command | Execute if false |
[[ c1 ]] && [[ c2 ]] | AND - both must be true |
[[ c1 ]] || [[ c2 ]] | OR - one must be true |
Conclusion
For a full list of test expressions you can use in your conditions, see our Bash comparison operators
guide. If you need to match a single variable against many values, the Bash case statement
is worth a look.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page