Learning this one feature means covering a huge chunk of Bash’s capabilities.


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 == B is 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

[[ "foo" == "bar" ]]

1 (false)

A equals B

(( 10 == 9 ))

1 (false)

A equals B

(( 10 != 9 ))

0 (true)

A not equals B

(( 10 > 9 ))

0 (true)

A greater than B

(( 10

1 (false)

A less than B

(( 10

1 (false)

A less than or equals B

(( 10 >= 9 ))

0 (true)

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 -le to compare numbers. Use ==, >, and to 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

==

-eq

Equal

!=

-ne

Not equals

>

-gt

Greater than

-lt

Less than

>=

-ge

Greater than or equal

-le

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

A && B

A || B

true

true

true

true

true

false

false

true

false

true

false

true

false

false

false

false

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


A terminal window displaying sample Bash script outputs, accompanied by shell and .sh icons.


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.

Lenovo ThinkPad X12 Gen 2 Detachable

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.



Source link

Leave a Reply

Subscribe to Our Newsletter

Get our latest articles delivered straight to your inbox. No spam, we promise.

Recent Reviews



Nothing has quietly fixed one of the most annoying aspects of Essential Space. The company has enabled cloud backup for content stored in the feature, meaning it is no longer tied to a single device. 

It will now travel with you, should you choose to switch from one Nothing or CMF device to another, synced via your Nothing account. 

Essential Space now stays with you.

Cloud storage keeps your notes, screenshots, voice captures, images, tasks and summaries backed up and synced through your Nothing account.

So when you move to a new phone or reset your device, your Space comes with you. pic.twitter.com/JSX4Ho4EYN

— Essential (@essential) April 27, 2026

What exactly is backed up?

Everything you’ve ever captured with the Essential Key is eligible for backup. This includes your audio recording, quick screenshots, saved images, email or document summaries — essentially the entire Essential Space content library. The feature also takes care of offline captures.

If auto-updates for apps are enabled in the Google Play Store, the app should receive the new feature automatically. However, if it doesn’t, you can update the app manually to enable cloud backup. 

Once the update is installed, you can head to Essential Space > Profile > Storage, and select Backup to set it up. The feature’s backend is based on Google’s cloud infrastructure (not Google Drive); it doesn’t count toward your personal Google storage quota.

Furthermore, the data remains fully GDPR-compliant, implying that only you can access the content.

Rolling out from today to all 2025–2026 Nothing and CMF phones that support the Essential Key.

Update Essential Space from the Google Play Store, or turn on auto-update to get it automatically.

— Essential (@essential) April 27, 2026

Which devices support the feature?

For now, cloud backup for Essential Space is rolling out to all 2025-2026 Nothing and CMF phones that feature the Essential Key. To my recollection, this includes the Nothing Phone (3), Phone (4a), Phone (4a) Pro, and the CMF Phone 2 Pro, among others. 

Older devices without the Essential Key are not supported, at least for now. A gap worth flagging is that there’s no web or desktop version of Essential Space, a fact the company has already acknowledged. 

For Nothing to create a functional ecosystem of devices, the Essential Space cloud backup is quite essential. Without it, every upgrade or device reset was a potential data loss event, but the cloud backup suggests that Nothing is on the right track. 



Source link