Are you pondering learning how to script on Linux? Perhaps you wanted to create a service or automate something, but thought Bash was too much. The if-statement represents a huge chunk of how a script functions, yet you can learn it in an hour. That knowledge opens up many new, strong options on Linux, so it’s worth taking the time to learn.
To learn Bash competently, you need only a few essential things, and they will carry you a very long way. Variables, functions, argument parsing, and if-statements. These are the foundational pillars, and today, I’ll cover the latter.
What is an if-statement in Bash?
A means to handle questions and direct program flow
Simply put, it’s a means to answer a question. Scripts make decisions, and the path through them gets determined by handling those questions. Each fork in the road is called a branch, and the questions themselves are conditions.
Let’s jump right in and look at a simple example:
# Compare non-numerical values
if [[ "foo" == "foo" ]]; then
echo "Foo equals foo"
fi
# Compare numbers
if (( 1 == 1 )); then
echo "One equals one"
fi
- “==” is a comparison operator (is equal to)
-
A == Bis a condition (question) - “echo” psrints a message when the condition is true
- “fi” ends the if-statement
This entire statement compares two values (which evaluates to true), then displays a message.
Notice how numbers and words (aka strings) have different bracket types? Parentheses (like ((...))) are for numbers; [[ ... ]] is typically not (more on that next).
Creating conditions
Put questions inside double brackets
“1 == 1” is called an expression—which means it evaluates to a value. For example, 1 + 1 evaluates to 2, and 1 == 1 to true.
There are several ways to create expressions in Bash, and for if-statements, we most often use comparison operators (like “==” or “>”). Here are some concrete examples.
|
Operator |
Evaluates to |
Description |
|---|---|---|
|
|
|
A equals B |
|
|
|
A equals B |
|
|
|
A not equals B |
|
|
|
A greater than B |
|
|
|
A less than B |
|
|
|
A less than or equals B |
|
|
|
A greater than or equals B |
In Bash, conditions evaluate to a number (aka exit status): “0” is true and non-zero is false. I will use true and false to make things clear.
Parentheses ((( ... ))) and square brackets ([[ ... ]]) treat comparison operators differently:
-
For square brackets: Use
-eq,-gt,-lt,-ge, and-leto compare numbers. Use==,>, andto compare strings. -
For parentheses: use
==,>,,, and>=to compare numbers. Never compare strings.
When comparing the letter forms (e.g., -gt) to the symbols used in the parentheses notation (e.g., (( a > b ))), this is what they mean:
|
Symbols |
Letters |
Description |
|---|---|---|
|
|
|
Equal |
|
|
|
Not equals |
|
|
|
Greater than |
|
|
Less than |
|
|
|
|
Greater than or equal |
|
|
Less than or equal |
These are the three valid ways to use them with the double-bracket notation:
# Compare strings
if [[ "foo" == "foo" ]]; then
echo "They're equal"
fi
# Compare numbers
if (( 2 >= 1 )); then
echo "Two is greater than or equal to one"
fi
# Compare numbers using square brackets
if [[ 2 -ge 1 ]]; then
echo "Two is greater than or equal to one"
fi
Creating multiple conditions
Use logical operators to chain conditions
Inside the brackets you can use logical operators to evaluate multiple conditions:
# "&&" means "and"
if (( 1 == 1 && 2 == 2 )); then
echo "Both conditions are true"
fi
# "||" means "or"
if (( 1 == 1 || 2 == 2 )); then
echo "At least one condition is true"
fi
“&&” and “||” work like their English labels: “and” and “or.” For example, one equals one and two equals two—when both are true, the entire statement is true.
To simplify understanding, this truth table captures all outcomes using the specified logical operators:
|
A |
B |
|
|
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Take the first row: when “A” is true and “B” is true, the expression A && B is true. A && B here is like writing true && true.
In summary, for such expressions to be true:
-
&&: Both must be true -
||: At least one must be true
All other conditions are false.
A third operator, “!”, flips a condition. It turns true into false, and vice versa:
if ! (( 1 == 1 )); then
echo "This never prints"
fi
To make it work in an expected way, place the “!” outside the brackets.
Bash first evaluates everything inside the brackets, then negates the result.
Single vs double bracket notation
The double-bracket notation is modern and more powerful
There are two ways to write Bash if-statements (single and double brackets):s
if [[ ... ]]; then
...
fi
if [ ... ]; then
...
fi
- Double brackets: The newer, more robust way that works in a few shells, including Bash
- Single brackets: The older, less robust way that works in different shells because it’s POSIX-compliant
I’d recommend the double-bracket notation, because most Linux systems include a Bash shell. In all my years using Linux, I’ve never had a single problem running a Bash script. Perhaps if you use Alpine Docker containers (which ship with ash), but that’s it. If you add a #!/bin/bash shebang at the top of your script, it will only execute in Bash.
There are some minor differences in how they handle logical operators:
if [[ 20 -ge 18 && 20 -lt 65 ]]; then
echo "Working age"
fi
if [ 20 -ge 18 ] && [ 20 -lt 65 ]; then
echo "Working age"
fi
The double-bracket notation also enables features like regex comparisons (=~), and it doesn’t split words. These are advanced topics, so skip them if uncomfortable.
Capturing multiple branches
Use if-else and if-elif-else to answer all possible questions
You will almost certainly need to handle multiple branches. To do that, use an if-else statement:
if (( 2 > 1 )); then
echo "Two is greater than one"
else
echo "Two is not greater than one"
fi
To add more branches, add as many “elif” lines as you need:
if (( 1 > 2 )); then
echo "One is greater than two"
elif (( 1 == 2 )); then
echo "One equals two"
else
echo "One is less than two"
fi
Capturing program status
Use commands without brackets to direct program flow
One great reason to use Bash for scripting is how cleanly it handles exit statuses. When a program fails, it returns a non-zero exit code; when it succeeds, it returns zero. We can act on that directly by leaving out the brackets:
if grep -q "root" /etc/passwd; then
echo "Found the root user"
else
echo "No root user found"
fi
3 Bash scripting techniques every Linux user should know
Unlock the power of Bash with these simple techniques.
We’ve covered significant ground on Bash if-statements, and what you’ve learned here will take you far. As already stated, they’re a huge chunk of how scripts work, so the time invested is worth the effort.
8/10
- Operating System
-
Windows 11
- CPU
-
Intel Core Ultra 5 & 7
- RAM
-
16GB or 32GB LPDDR5X 6400MHz
The Lenovo ThinkPad X12 Gen 2 Detachable is one of the company’s latest Windows 11-based tablets. Featuring Intel Core Ultra x86 processors, the ThinkPad X12 Gen 2 delivers all-day battery life, enough power to handle daily workflow tasks, and a solid keyboard experience. Plus, the included keyboard is backlit and you’ll also get a bundled stylus.
I’ll direct you to the official Bash manual for in-depth learning. It’s heavy reading, so I don’t recommend you read it from cover to cover, but use it as a reference for the finer details instead.

