Master Bash Conditional Tests: File, String, and Numeric Checks Explained
Learn how to use Bash's test command and its various syntaxes—including [, [[, and ((—to perform file existence checks, string evaluations, numeric comparisons, and logical operations—complete with practical examples and common operators for effective shell scripting.
Technical advancement is merely quantitative accumulation; intellectual growth is a qualitative leap. — Lao Nian Hai
You can view the test command manual with man test.
Test Statement Syntax
The test command can be written in four forms:
test expression [ expression ] [[ expression ]] (( expression ))Forms 1 and 2 have essentially the same functionality with different syntax. Form 3 supports pattern matching with wildcards, while form 4 is mainly for integer comparisons and arithmetic.
Condition Test Types
File tests
Numeric tests
String tests
Logical tests
File Tests
Common file test operators:
-b : file exists and is a block device
-c : file exists and is a character device
-d : file exists and is a directory
-e : file exists
-f : file exists and is a regular file
-s : file exists and its size is greater than zero
-L : file exists and is a symbolic link
-r : file exists and is readable
-w : file exists and is writable
-x : file exists and is executable
file1 -nt file2 : file1 is newer than file2
file1 -ot file2 : file1 is older than file2
Examples:
# Create a regular file
touch wanger
# Check if it is a regular file
test -f wanger && echo 0 || echo 1
# Check if it is a directory
[ -d wanger ] && echo 0 || echo 1
# Create another file
touch wangerxiao
# Compare modification times
test wanger -nt wangerxiao && echo 0 || echo 1
[ wanger -ot wangerxiao ] && echo 0 || echo 1String Tests
Common string test operators:
-z string : true if the length of string is zero
-n string : true if the length of string is non‑zero
string1 = string2 : true if the strings are equal
string1 != string2 : true if the strings are not equal
Examples:
# Define a variable
name=wanger
# Test if the variable is empty
[ -z $name ] && echo 0 || echo 1
# Test if the variable is non‑empty
test -n $name && echo 0 || echo 1
# Compare variable with a literal
[ $name != "wanger" ] && echo 0 || echo 1
test $name = "wanger" && echo 0 || echo 1Numeric Tests
Common numeric test operators:
-eq : equal
-ne : not equal
-gt : greater than
-ge : greater than or equal
-lt : less than
-le : less than or equal
Note: These operators cannot be used inside (( )); within (( )) you should use < > = != >= <= for comparisons.
Examples:
# Check if 6 is less than or equal to 5
test 6 -le 5 && echo 0 || echo 1
# Check if 6 is greater than 5
[ 6 -gt 5 ] && echo 0 || echo 1
# Check equality with [[ ]]
[[ 6 = 5 ]] && echo 0 || echo 1
# Arithmetic comparison
((5!=6)) && echo 0 || echo 1Logical Operators
-a : logical AND (equivalent to &&)
-o : logical OR (equivalent to ||)
! : logical NOT
Examples:
# Define a variable
test="123"
# Check if variable is non‑empty OR equals "123"
[ ! -z $test -o $test = "123" ] && echo 0 || echo 1
# Using && and || (only -a and -o are allowed inside [ ])
[ -z $test ] && test $test = "123" && echo 0 || echo 1
# Using [[ ]] with ||
[[ -z $test || $test = "123" ]] && echo 0 || echo 1For the full original notes, click the original article link or view the learning notes in the menu.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
