The test Command
In this page:
The test Command
test expression (also written as [ expression ]) evaluates a condition and sets its exit status accordingly, forming the classic POSIX way of writing conditions. [ ] requires spaces around every token and around the brackets themselves. It predates the more feature-rich [[ ]] and is still used for POSIX-portable scripts.
Warning: Missing a space, like [$a -eq $b], is a syntax error — [ is actually a command that needs spaces as argument separators.
Example: The test Command
#!/bin/bash
a=5
if test "$a" -eq 5; then
echo "a equals 5 (using test)"
fi
if [ "$a" -eq 5 ]; then
echo "a equals 5 (using [ ])"
fi
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: