Skip to content

Bash If/Else Examples

conditionalsbasicsif
5 min read

Quick Answer

A bash if statement tests whether a command exits with code 0 (success) or non-zero (failure). The test command — written as [ condition ] — evaluates comparisons and file checks. The full structure is: if [ condition ]; then ... elif [ condition ]; then ... else ... fi. Spaces inside the brackets are mandatory. For integers use -eq (equal), -gt (greater than), -lt (less than). For strings use = and !=. For files use -f (regular file exists), -d (directory exists), -e (either exists). A common mistake is using = for numbers — [ 5 = 10 ] does string comparison and gives unpredictable results with numbers. Always quote variables: [ "$VAR" = "value" ] handles empty strings safely where [ $VAR = "value" ] would cause a syntax error. The test command and if syntax are core bash, so this runs unmodified on every Linux distribution and on macOS.

A small scheduled job I'd half-forgotten about stopped doing its work, and it did it the worst possible way — silently, with no error I'd ever see, because it was running under cron with its output going nowhere. When I finally dug in, the culprit was a single test I'd written carelessly: [ $STATUS = ok ], no quotes. As long as $STATUS came back with a value it was fine. The one time the command feeding it returned nothing, $STATUS expanded to empty, the test collapsed to [ = ok ], and bash choked on a comparison with a missing left-hand side.

That's the whole trap, and it's why every example here quotes its variables. An unquoted variable in [ ] isn't one argument that might be empty — when it's empty it's zero arguments, so the shell sees [ = ok ] and either errors or evaluates something you never meant. Wrap it in quotes and an empty value becomes [ "" = ok ]: still false, but valid, and your branch behaves. The three-branch disk check below is where I rebuilt my intuition for if/elif/else, the comparison operators that actually matter, and the spacing rules bash is unforgiving about — the same small things that, left unquoted, take down a job you'd stopped thinking about. A job dying in silence over a missing pair of quotes is not something I wanted to debug twice.

The three-branch check this all started with

Copy this into disk-if-else.sh. It uses if, elif, and else to classify disk usage into three levels — the same pattern as our disk space warning snippet, with an extra critical branch.

bash
#!/bin/bash # Script: disk-if-else.sh # Purpose: Classify disk usage into critical, warning, or OK — three-branch if/elif/else # Usage: ./disk-if-else.sh set -euo pipefail CHECK="✓" CROSS="✗" THRESHOLD=80 CRITICAL=90 PARTITION="/" USAGE=$(df "$PARTITION" | awk 'NR==2{print $5}' | tr -d '%') if [ "$USAGE" -gt "$CRITICAL" ]; then echo "$CROSS CRITICAL: Disk at ${USAGE}% — free space immediately" elif [ "$USAGE" -gt "$THRESHOLD" ]; then echo "$CROSS WARNING: Disk at ${USAGE}% — above ${THRESHOLD}% threshold" else echo "$CHECK OK: Disk at ${USAGE}% (warn: ${THRESHOLD}%, critical: ${CRITICAL}%)" fi

What this does, line by line

THRESHOLD and CRITICAL set the two cutoffs. USAGE reads the current disk percent from df. The first if handles the worst case. elif runs only when the first test failed — still high, but not critical. else covers everything else. fi closes the block — always required.

Building the check yourself

Step 1 — Create the file

Open a terminal and run:

bash
nano disk-if-else.sh

Paste the script above, then press Ctrl+X → Y → Enter to save.

Step 2 — Understand the if/elif/else shape

Every branch follows the same pattern — spaces matter:

PartSyntaxPurpose
ifif [ condition ]; thenFirst test — runs when true
elifelif [ condition ]; thenSecond test — only if previous tests failed
elseelseFallback — runs when no test matched
fifiCloses the entire block — do not forget this

Step 3 — Make it executable

bash
chmod +x disk-if-else.sh

You only need to do this once. It gives the script permission to run.

Step 4 — Run it

bash
./disk-if-else.sh

You should see one of three messages depending on how full your disk is right now.

Schedule It with Cron

Disk checks only help if they run automatically. Add the script to cron so you catch problems before the drive fills.

Open your crontab

bash
crontab -e

Add one of these lines

bash
# Check every day at 8am 0 8 * * * /home/user/disk-if-else.sh # Check every hour 0 * * * * /home/user/disk-if-else.sh # Log output to a file 0 8 * * * /home/user/disk-if-else.sh >> /var/log/diskcheck.log 2>&1

Tip: Use crontab.guru

Go to crontab.guru to build and test cron time expressions for free. It explains exactly when your job will run in plain English.

The comparisons you'll actually reach for

String comparison (= and !=)

Use = and != inside [ ] for text. Quote variables so empty values do not break the test:

bash
#!/bin/bash ENV="production" if [ "$ENV" = "production" ]; then echo "Running production config" elif [ "$ENV" = "staging" ]; then echo "Running staging config" else echo "Unknown environment: $ENV" fi

Check if a file exists (-f)

-f returns true only for regular files. Use -d for directories and -e for either:

bash
#!/bin/bash CONFIG="/etc/myapp/config.yml" if [ -f "$CONFIG" ]; then echo "Config found — loading $CONFIG" else echo "Config missing — creating default at $CONFIG" touch "$CONFIG" fi

Integer comparison (-eq, -gt, -lt)

Numbers inside [ ] need numeric operators, not =:

bash
#!/bin/bash COUNT=42 if [ "$COUNT" -eq 0 ]; then echo "No items" elif [ "$COUNT" -lt 10 ]; then echo "Low count: $COUNT" else echo "Count is $COUNT" fi

Bash Comparison Operator Quick Reference

TypeOperatorMeaning
Integer-eqEqual
Integer-neNot equal
Integer-gtGreater than
Integer-ltLess than
Integer-geGreater than or equal
Integer-leLess than or equal
String=Equal
String!=Not equal
String-zEmpty string
String-nNon-empty string
File-fRegular file exists
File-dDirectory exists
File-eFile or directory exists

The bracket traps everyone hits

The trap that actually bit me lives one level below these: an unquoted variable in a test. [ $x = ok ] works until $x is empty, at which point it collapses to [ = ok ] and bash errors on the missing operand. Quoting it — [ "$x" = ok ] — turns an empty value into a harmless empty string instead of a vanished argument. The three below are the ones that break scripts most often.

Forgetting spaces inside [ ]

Bash requires spaces around brackets and operators. if [ "$x" -eq 5 ] works. if ["$x"-eq 5] fails with a syntax error. The [ command needs each piece as a separate argument.

Using = instead of -eq for numbers

Use = for string comparisons. For integers use -eq, -ne, -gt, -lt. Writing if [ "$USAGE" = 80 ] can behave unexpectedly — use -gt for greater-than checks like disk percentages.

Missing fi at the end

Every if block must end with fi (if spelled backwards). Without it, bash reports syntax error: unexpected end of file. Nested ifs need a fi for each level.

Frequently Asked Questions

How do I write an if/else statement in bash?

The basic structure is: if [ condition ]; then ... else ... fi. The spaces around the condition inside [ ] are mandatory — [ "$x" = "y" ] works, ["$x" = "y"] does not.

What is the difference between [ ] and [[ ]] in bash?

[ ] is POSIX-compliant and works in all shells. [[ ]] is a bash extension with additional features: regex matching with =~, no word splitting on unquoted variables, and &&/|| instead of -a/-o. Use [[ ]] in bash-only scripts when you need those features.

How do I compare numbers in a bash if statement?

Use integer comparison operators inside [ ]: -eq (equal), -ne (not equal), -gt (greater than), -lt (less than). Do not use = for numbers — [ 5 = 10 ] does string comparison and can give wrong results.

How do I check if a variable is empty in bash?

Use [ -z "$VAR" ] to test for empty, or [ -n "$VAR" ] for non-empty. Always quote the variable — without quotes, an empty string causes a syntax error in the test.

Why does bash say "command not found" inside an if statement?

The most common cause is a missing then keyword or unquoted variable. Every if line must end with ; then. Check for: missing spaces inside [ ], missing fi at the end, and unquoted variables that contain spaces.

BashSnippets logo

Written by Anguishe

Creator of BashSnippets.xyz

bashsnippets.xyz/about

Run this script on a real Linux server

Get $200 free credit — DigitalOcean

Get $200 Free →

Affiliate link · we earn a commission

Need a domain for your next project?

Register with Namecheap — free WHOIS privacy included

Check Domain Prices →

Affiliate link · we earn a commission

PAID RESOURCE — $9

The Production Bash Toolkit

6 scripts + shared library + 52-page field guide. The production layer the free snippets don't cover.

Get the Toolkit →

Related Snippets

Frequently Asked Questions

faq — snippet

How do I run this script?

Save the if/else examples as conditionals.sh, run chmod +x conditionals.sh, then execute ./conditionals.sh.

faq — snippet

Does this work on macOS?

Yes. [ ] and [[ ]] test syntax works on macOS bash and zsh. Quote all variables inside brackets.

faq — snippet

How do I write an if/else statement in bash?

Use if [ condition ]; then ... elif [ condition ]; then ... else ... fi. Spaces inside brackets are mandatory.

faq — snippet

How do I compare numbers in a bash if statement?

Use -eq, -gt, -lt for integers inside [ ]. Never use = for numeric comparison — that does string comparison.