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
#!/bin/bash
if test 5 -eq 5; then
echo "test command: equal"
fi
if [ 5 -eq 5 ]; then
echo "[ ] form: equal"
fi
Login to try C/C++/Java/PHP code in the editor
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
#!/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
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
name="ada"
if [ "$name" = "ada" ]; then
echo "Name matches"
fi
if [ "$name" != "grace" ]; then
echo "Name is not grace"
fi
Login to try C/C++/Java/PHP code in the editor
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
#!/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
Login to try C/C++/Java/PHP code in the editor
- Forgetting the required spaces around
[and];[$x -eq 5]is a syntax error, it must be[ $x -eq 5 ]with spaces on both sides. - 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. - Not quoting variables inside
[ ], e.g.[ $name = "ada" ]; if$nameis empty or contains spaces, this can turn into a syntax error rather than a false condition.
[ ]is literally thetestcommand;[ expr ]andtest exprare 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,-echeck 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.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: