Bash if, elif, and else Statements: Syntax and Examples

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 -r 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 [[ ... ]] expression 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 -r 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 script and enter 7, the condition is false, so Bash skips the then block and runs the else block instead:
Enter a number: 7
The variable is less than or 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.
Bash does not support the else if syntax used in languages such as C or JavaScript. Use the elif keyword instead, which is short for “else if”.
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 -r 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."
fiThis time, entering 10 fails the first test and matches the second one, so the script prints the elif message and never reaches else:
Enter a number: 10
The variable is equal to 10.Nested if Statements
Bash allows you to nest if statements within if statements, and you can place as many of them inside another if statement as you need.
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 -r VAR1
echo -n "Enter the second number: "
read -r VAR2
echo -n "Enter the third number: "
read -r 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 -r VAR1
echo -n "Enter the second number: "
read -r VAR2
echo -n "Enter the third number: "
read -r 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."
fiThe first condition is true only when VAR1 is greater than or equal to both other numbers. The elif line applies the same check to VAR2, and the else branch catches the case where VAR3 is the largest.
Inside [[ ]], you can also put the && and || operators directly between the expressions instead of chaining two separate tests. The following script accepts both a lowercase and an uppercase answer:
#!/bin/bash
read -r -p "Continue? [y/n]: " ANSWER
if [[ "$ANSWER" = "y" || "$ANSWER" = "Y" ]]
then
echo "Continuing."
else
echo "Aborted."
fiIf you type Y, the second comparison is true, and that is enough for the whole condition to pass:
Continue? [y/n]: Y
Continuing.Any other answer, including an empty one, makes both comparisons false, and the script runs the else branch. With &&, both sides must be true. For example, [[ $VAR -gt 1 && $VAR -lt 10 ]] checks whether a number is between 2 and 9.
Using Commands as Conditions
The TEST-COMMAND in an if statement does not have to be a bracket test. It can be any command. Bash runs the command and looks at its exit status
: an exit status of 0 means true, and any other value means false. The [[ ]] construct works the same way: it returns 0 or 1 depending on the expression.
This is why you can use a command such as grep
directly in the condition. The -q option tells grep to print nothing and only set the exit status:
#!/bin/bash
if grep -q "^root:" /etc/passwd
then
echo "The root user exists."
fiThe grep command finds a line that starts with root:, exits with 0, and the script prints the message:
The root user exists.To run a block when a condition is false, put the ! operator in front of it. The following script creates a directory only if it does not already exist:
#!/bin/bash
DIR="/tmp/backup"
if [[ ! -d "$DIR" ]]
then
echo "Creating $DIR"
mkdir -p "$DIR"
fiThe first time you run the script, it prints Creating /tmp/backup. On the second run, the directory exists, the negated test is false, and the script prints nothing. The ! operator works with commands too, so if ! grep -q "^john:" /etc/passwd runs the block when no matching line is found or when grep fails. Normally, grep returns 1 for no match and 2 for an error, such as an unreadable file. The ! operator treats both statuses the same, so the block running does not by itself prove that the user is missing.
Test 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 ]]The single bracket [ is the same command as test, and it is available in every POSIX shell, including sh and dash. The double bracket [[ construct is a Bash keyword (also supported by Zsh and Ksh) that does not split words or expand globs on unquoted variables, and it supports &&, ||, and pattern matching inside the brackets. Use [[ in Bash scripts, as the examples in this guide do, and switch to [ only when the script must run under a plain POSIX shell.
For numeric comparisons, Bash also has the (( )) arithmetic
construct, which lets you use the familiar >, <, >=, <=, and == operators:
#!/bin/bash
COUNT=15
if (( COUNT > 10 ))
then
echo "COUNT is greater than 10."
fiInside (( )), you do not need the $ before variable names, and the condition is true when the expression evaluates to a non-zero value.
To negate the test expression, use the logical NOT (!) operator. When comparing strings
, use double quotes around variable expansions, such as "$VAR". Single quotes preserve literal text and prevent variables from expanding. Inside [[ ]], variables do not undergo word splitting or filename expansion; quote the right-hand side of = or != for literal comparisons.
Below are some of the most commonly used operators:
-n "$VAR"- True if the length ofVARis greater than zero.-z "$VAR"- 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 thanINTEGER2.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 -r -p "Enter your username: " NAME
if [[ "$NAME" = "admin" ]]
then
echo "Welcome, admin."
else
echo "Access denied."
fiCheck whether a variable is empty:
#!/bin/bash
read -r -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.Troubleshooting
Bash if statements fail with a few characteristic errors. Here is how to fix the most common ones.
syntax error near unexpected token ’then’
The then keyword is on the same line as the condition without a separator. Add a semicolon before then (if [[ $VAR -gt 10 ]]; then) or place then on its own line.
unary operator expected
With the single bracket [, an unquoted empty variable disappears during expansion, so [ $VAR = "abc" ] becomes [ = "abc" ] and cannot be parsed. Always quote variables inside [ ], or use [[ ]], which handles unset and empty variables without word splitting.
[[: not found
The script is running under a POSIX shell such as sh or dash instead of Bash. The [[ construct is a Bash keyword and does not exist in plain sh. Run the script with bash script.sh, or add #!/bin/bash as the first line, make the file executable, and run it as ./script.sh.
command not found errors mentioning the bracket
The [ is a command and requires spaces around it and before the closing ]. Write [ "$VAR" -gt 10 ], not ["$VAR" -gt 10].
Quick Reference
For a printable quick reference, see the Bash cheatsheet .
| 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 |
[[ c1 && c2 ]], [[ c1 || c2 ]] | AND and OR inside the brackets |
if [[ ! condition ]]; then ...; fi | Run if the condition is false |
if command; then ...; fi | Run if the command exits with 0 |
if (( a > b )); then ...; fi | Arithmetic comparison |
Conclusion
Every if statement in Bash comes down to an exit status: the condition is a command, and 0 means true. 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 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page