Introduction#
Hello! In this article, we’ll learn about the test command.
test is a command for evaluating conditional expressions. Actually, the [ ] used in if statements is this test command. Many people don’t know that at first. I didn’t either.
What is the test Command?#
test is a command that evaluates conditional expressions and determines if they’re true or false.
The evaluation result is returned as an exit status. True is 0, false is 1 (same as regular success/failure).
Basic Syntax#
Using test Command#
Using [ ] (More Common)#
Actually [ is like an alias for test.
Using [[ ]] (Bash Extension)#
This one has more features (but bash-specific).
Main Conditional Expressions#
String Tests#
| Expression |
Meaning |
[ "$a" = "$b" ] |
Strings are equal |
[ "$a" != "$b" ] |
Strings are not equal |
[ -z "$a" ] |
String is empty |
[ -n "$a" ] |
String is not empty |
Numeric Tests#
| Expression |
Meaning |
[ $a -eq $b ] |
Equal |
[ $a -ne $b ] |
Not equal |
[ $a -gt $b ] |
Greater than |
[ $a -lt $b ] |
Less than |
[ $a -ge $b ] |
Greater or equal |
[ $a -le $b ] |
Less or equal |
File Tests#
| Expression |
Meaning |
[ -e "$file" ] |
File or directory exists |
[ -f "$file" ] |
Regular file exists |
[ -d "$dir" ] |
Directory exists |
[ -r "$file" ] |
Readable |
[ -w "$file" ] |
Writable |
[ -x "$file" ] |
Executable |
[ -s "$file" ] |
File size greater than 0 |
[ -L "$file" ] |
Symbolic link |
Usage Examples#
Example 1: Evaluate with test Command#
Output:
5 is greater than 3, so returns true (0).
Example 2: Evaluate with [ ]#
Output:
Same as before. test and [ ] mean the same thing.
Example 3: Combine with if Statement#
1
2
3
4
5
6
7
|
file="test.txt"
if [ -f "$file" ]; then
echo "$file exists"
else
echo "$file does not exist"
fi
|
Common to use inside if statements.
Example 4: Use with && and ||#
1
|
[ -f "test.txt" ] && echo "File exists" || echo "File doesn't exist"
|
If true, left side executes; if false, right side. Concise and convenient.
Example 5: Check if String is Empty#
1
2
3
4
5
|
name=""
if [ -z "$name" ]; then
echo "Name not entered"
fi
|
Output:
Example 6: Numeric Comparison#
1
2
3
4
5
|
score=85
if [ $score -ge 80 ]; then
echo "Pass"
fi
|
Output:
Example 7: Multiple Conditions (AND)#
1
2
3
4
5
|
age=25
if [ $age -ge 18 ] && [ $age -lt 65 ]; then
echo "Adult and working age"
fi
|
Example 8: Multiple Conditions (OR)#
1
2
3
4
5
|
ext="jpg"
if [ "$ext" = "jpg" ] || [ "$ext" = "png" ]; then
echo "Image file"
fi
|
Example 9: Negation#
1
2
3
|
if [ ! -f "config.txt" ]; then
echo "Config file missing"
fi
|
! inverts the condition.
Tips & Notes#
-
Spaces around [ ] required: Common mistake
1
2
|
[ $a -eq 1 ] # Correct
[$a -eq 1] # Error!
|
-
Quote variables: For empty string and whitespace safety
1
2
|
[ "$var" = "test" ] # Safe
[ $var = "test" ] # Error if variable is empty
|
-
Check with exit status: Can check with $?
1
2
3
4
5
|
[ 5 -gt 3 ]
echo $? # 0 (true)
[ 5 -lt 3 ]
echo $? # 1 (false)
|
-
Difference between test and [: Basically the same, but [ requires closing ]
1
2
|
test -f file.txt # OK
[ -f file.txt ] # OK (] required)
|
-
[[ ]] has more features: Recommended if using bash
1
2
3
4
5
|
# [[ ]] supports regex
[[ "hello" =~ ^h ]] && echo "Starts with h"
# Pattern matching
[[ "test.txt" == *.txt ]] && echo "txt file"
|
Practical Usage#
File Existence Check#
1
2
3
4
5
6
7
8
9
10
|
#!/bin/bash
config="/etc/myapp/config.conf"
if [ ! -f "$config" ]; then
echo "Error: Config file not found: $config"
exit 1
fi
echo "Loading config file: $config"
|
Argument Check#
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#!/bin/bash
if [ $# -eq 0 ]; then
echo "Usage: $0 <filename>"
exit 1
fi
if [ ! -f "$1" ]; then
echo "Error: File does not exist: $1"
exit 1
fi
echo "Starting process: $1"
|
Create Directory (Only if Not Exists)#
1
2
3
4
5
6
7
|
#!/bin/bash
backup_dir="./backup"
[ ! -d "$backup_dir" ] && mkdir -p "$backup_dir"
echo "Backup directory: $backup_dir"
|
Multiple Condition Check#
1
2
3
4
5
6
7
8
9
10
|
#!/bin/bash
file="data.txt"
if [ -f "$file" ] && [ -r "$file" ] && [ -s "$file" ]; then
echo "File exists, is readable, and is not empty"
cat "$file"
else
echo "File has problems"
fi
|
Environment Variable Check#
1
2
3
4
5
6
7
8
|
#!/bin/bash
if [ -z "$HOME" ]; then
echo "Error: HOME environment variable not set"
exit 1
fi
echo "Home directory: $HOME"
|
Numeric Range Check#
1
2
3
4
5
6
7
8
9
|
#!/bin/bash
read -p "Enter number 1-10: " num
if [ $num -ge 1 ] && [ $num -le 10 ]; then
echo "Within range"
else
echo "Out of range"
fi
|
Summary#
In this article, we learned about the test command.
Key Points:
test is a command for evaluating conditional expressions
[ ] means the same as test
- Can test various conditions: strings, numbers, files, etc.
- Use in combination with
if statements and &&/||
Realizing that the [ ] you normally use in if statements is actually this test command kind of clicks, right? It’s fundamental to conditional judgment, so remembering various conditional expressions is useful.
Keep learning Linux commands!
Related Articles#