← Back to Bash Course | Chapter 3: Control Flow | Lesson 4 of 14

test and [ ] Operators

[ ] (and the equivalent test command) is the classic, POSIX-standard way to check conditions like whether two strings are equal or a number is bigger than another.

[ ] Is the test Command

[ expression ] is literally a call to the test command, where [ is a program/builtin that requires a matching ] as its final argument. This is why spaces around both brackets are mandatory -- they are separate arguments, not decorative syntax.

Example: [ ] Is the test Command

bash
#!/bin/bash
if test 5 -eq 5; then
    echo "test command: equal"
fi
if [ 5 -eq 5 ]; then
    echo "[ ] form: equal"
fi

Numeric Comparisons

Inside [ ], numeric comparisons use named operators: -eq (equal), -ne (not equal), -gt (greater than), -ge (greater or equal), -lt (less than), and -le (less or equal). Using > or < here instead performs shell redirection, not comparison, which is a common mistake.

Example: Numeric Comparisons

bash
#!/bin/bash
a=10
b=20
if [ "$a" -lt "$b" ]; then
    echo "$a is less than $b"
fi
if [ "$a" -ne "$b" ]; then
    echo "$a is not equal to $b"
fi

String Comparisons

Inside [ ], string equality uses a single = (though == also works as a Bash extension) and inequality uses !=. String comparisons check character-by-character equality, not numeric value, so "5" = "05" would be false even though they represent the same number.

Example: String Comparisons

bash
#!/bin/bash
name="ada"
if [ "$name" = "ada" ]; then
    echo "Name matches"
fi
if [ "$name" != "grace" ]; then
    echo "Name is not grace"
fi

File Test Operators

[ ] also supports file test operators like -e (path exists), -f (is a regular file), -d (is a directory), and -r/-w/-x (readable/writable/executable). These are essential for scripts that check the filesystem before acting on it.

Example: File Test Operators

bash
#!/bin/bash
touch sample.txt
if [ -e sample.txt ]; then
    echo "sample.txt exists"
fi
if [ -f sample.txt ]; then
    echo "sample.txt is a regular file"
fi
if [ ! -d sample.txt ]; then
    echo "sample.txt is not a directory"
fi
Common Mistakes
  1. Forgetting the required spaces around [ and ]; [$x -eq 5] is a syntax error, it must be [ $x -eq 5 ] with spaces on both sides.
  2. Using == or >/< for numeric comparisons inside [ ]; those are string operators there (and >/< need escaping to avoid being treated as redirection) -- numeric comparison needs -eq, -gt, -lt, etc.
  3. Not quoting variables inside [ ], e.g. [ $name = "ada" ]; if $name is empty or contains spaces, this can turn into a syntax error rather than a false condition.
Chapter Summary
  • [ ] is literally the test command; [ expr ] and test expr are equivalent, [ is just an alias that requires a matching ].
  • String comparisons use = and !=; numeric comparisons use -eq, -ne, -gt, -ge, -lt, -le.
  • File tests like -f, -d, -e check whether a path is a regular file, a directory, or exists at all.
  • Always quote variables inside [ ], e.g. [ "$var" = "value" ], to avoid errors when the variable is empty or contains spaces.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.