HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to BashNumbers & ArithmeticStringsArraysConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Arithmetic
✍️ Text
📋 Array
🔁 Condition & Loop
🧩 Functions & Pipes
Bash›Conditions (if)

Conditions (if)

Decision making and test operators (-eq, -lt, =) with if [ ].

Simple enough for a 5-year-old to understand

if / else

Tests are performed inside square brackets; spaces around [ and ] are MANDATORY.

Example code
1
score=75
2
if [ $score -ge 50 ]; then
-ge = greater than/equal.
Condition-ge = greater than/equal.
3
echo "Passed"
4
else
5
echo "Failed"
6
fi
End of if (written backwards).
ConditionEnd of if (written backwards).
Condition
💡 Hint: Number tests: -eq -ne -lt -gt -le -ge. Text: = !=. Spaces inside [ ] are mandatory.

About this topic

Conditions in Bash work fundamentally differently from other languages. if tests the exit code of a COMMAND, not a value. An exit code of zero means success, and any non-zero value means failure — meaning "true" is zero, which is the exact opposite of many languages.

The square bracket is not a syntax element, it is a COMMAND. That is why spaces inside it are mandatory and its arguments must be quoted. This single concept explains the vast majority of errors in Bash conditions.

Bash offers two test formats. Single square brackets are POSIX-compliant but break when a variable is empty; double square brackets are Bash-specific, safe with empty values, and can do pattern matching. Double square brackets are preferred in Bash scripts.

What you will learn

  • the if and elif structure
  • The exit code logic: zero means success
  • That spaces are mandatory inside square brackets
  • That numeric and string comparison operators are separate
  • Why double square brackets are preferred

When to use?

The test command is used for file existence, directory checks, and string comparisons; there is a separate option for each.

To test whether a command was successful, it is enough to put it directly inside if; capturing the exit code into a variable and comparing it is unnecessary.

Common mistakes

Wrong

a=5
if [$a -gt 3]; then
  echo "Greater"
fi

Square brackets are a command and the command name must be separated from its arguments by a space; because there is no space, the shell gives the error "[5: command not found". The space before the closing bracket is also required because it is an argument as well.

Correct

a=5
if [ "$a" -gt 3 ]; then
  echo "Greater"
fi

Wrong

name=""
if [ $name = "Ali" ]; then
  echo "Hello"
fi

Because the variable is empty and unquoted, the expansion produces no arguments at all; the test command is called with a missing argument and gives the error "unary operator expected". Adding double quotes or using double square brackets solves the issue.

Correct

name=""
if [[ "$name" == "Ali" ]]; then
  echo "Hello"
fi

Wrong

a=10
b=9
if [[ "$a" > "$b" ]]; then
  echo "a is greater"
fi

The greater-than sign inside double square brackets performs a STRING comparison: alphabetically the value "10" is smaller than the value "9", so the condition is not met. Letter operators should be used for numeric comparisons; this distinction is a classic source of silent bugs.

Correct

a=10
b=9
if [[ "$a" -gt "$b" ]]; then
  echo "a is greater"
fi

Frequently asked questions

Why is zero considered true?

Because an exit code is an error number: zero means "no error", and non-zero values report different types of errors. This tradition comes from Unix and all commands follow it. It is about reporting success, not logical truth.

Should I use single or double square brackets?

Double in a Bash script. It doesn't break with empty variables, can do pattern matching and regular expression testing, and logical operators can be written more naturally inside it. Single square brackets are only required when the script also needs to run in POSIX shells.

How do I test the success of a command?

By putting the command directly inside if. The exit code is already used as the condition; reading the exit code from a special variable and comparing it to zero is an unnecessary step and gives wrong results if another command sneaks in between.

Summary

if tests the exit code of a command, not a value; zero means success.

Square brackets are a command, spaces inside them are mandatory.

The operators for numeric and string comparisons are different.

Double square brackets are safe with empty values and are preferred.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code