Quiz Entry - updated: 2026.07.05
What is the test command and how does [ ] syntax work?
[ ... ] is just another name for the test command — it evaluates a condition and exits 0 (true) or 1 (false). The spaces inside the brackets are mandatory.
The mind-bending part: [ is a command, not punctuation. [ -f file ] is literally the program [ being called with arguments -f, file, and a closing ]. That's why the spaces are non-negotiable — [-f file] makes the shell look for a command named [-f, and [ -f file] drops the ] argument the command requires. Seeing [ as a command also explains why it returns an exit code that if can test.
test -f /etc/passwd # long form
[ -f /etc/passwd ] # identical — '[' is the same command as 'test'
Important: [ is actually a command! Spaces are required:
# Correct
[ -f /etc/passwd ]
# WRONG - syntax errors
[-f /etc/passwd] # No space after [
[ -f /etc/passwd] # No space before ]
Use in if statements:
if [ -f /etc/passwd ]; then
echo "File exists"
fi
# Or use test directly
if test -f /etc/passwd; then
echo "File exists"
fi
Return values:
- Returns 0 (true) if condition is met
- Returns 1 (false) if condition is not met
Go deeper:
Bash Guide: Tests and Conditionals (Wooledge) —
[is thetestcommand, why the spaces are mandatory, and the operator set.