Fundamentals 12 min read

Mastering the Bash test Command: Compare Numbers, Strings, and Files

This guide explains how to use the Bash built‑in test command for evaluating numeric, string, and file conditions, shows practical examples with && and || chaining, and demonstrates alternative bracket syntax for robust shell scripting.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering the Bash test Command: Compare Numbers, Strings, and Files

What is the test command?

The test command is a Bash built‑in used to evaluate whether a condition is true, typically within if statements. It can compare two elements and returns an exit status rather than printing a value.

A basic example

Run the following in a terminal to see the result: test 1 -eq 2 && echo "yes" || echo "no" Output:

no
test 1 -eq 1 && echo "yes" || echo "no"

Output: yes The command breakdown:

test : performs the comparison

1 : first operand

-eq : equality operator

2 : second operand

&& : executes the next command only if the previous one succeeded (exit status 0)

echo "yes" : runs when the comparison is true

|| : runs the following command only if the previous one failed (non‑zero exit status)

echo "no" : runs when the comparison is false

Note: test itself does not produce output; it only sets the exit status, which is why echo is chained to display a result.

Numeric comparisons

Use these operators when both operands are numbers:

-eq : equal

-ne : not equal

-gt : greater than

-ge : greater than or equal

-lt : less than

-le : less than or equal

Examples

test 1 -eq 2 && echo "yes" || echo "no"   # prints no
test 1 -ge 2 && echo "yes" || echo "no"   # prints no
test 1 -gt 2 && echo "yes" || echo "no"   # prints no
test 1 -le 2 && echo "yes" || echo "no"   # prints yes
test 1 -lt 2 && echo "yes" || echo "no"   # prints yes
test 1 -ne 2 && echo "yes" || echo "no"   # prints yes

String comparisons

When comparing text, use these operators:

= : strings are equal

!= : strings are not equal

-n : string length is non‑zero

-z : string length is zero

Examples

test "string1" = "string2" && echo "yes" || echo "no"   # prints no
test "string1" != "string2" && echo "yes" || echo "no"   # prints yes
test -n "string1" && echo "yes" || echo "no"           # prints yes
test -z "string1" && echo "yes" || echo "no"           # prints no

File attribute comparisons

Use these operators to test file properties:

-e : file exists

-f : regular file

-d : directory

-r : readable

-w : writable

-x : executable

-L or -h : symbolic link

-nt : newer than

-ot : older than

-ef : same inode (identical file)

Examples

test linuxmi -nt linux && echo "yes"

(prints “yes” if *linuxmi* is newer than *linux*) test -e /home/linuxmi/linuxmi && echo "yes" (prints “yes” if the file exists) test -O /home/linuxmi/linuxmi && echo "yes" (prints “yes” if you own the file)

Testing multiple conditions

Combine tests with logical AND ( -a ) or OR ( -o ), or chain separate test commands using && and ||:

test 4 -eq 4 -a "moo" = "moo" && echo "it is a cow" || echo "it is not a cow"

Better style:

test 4 -eq 4 && test "moo" = "moo" && echo "it is a cow" || echo "it is not a cow"

OR example:

test -e linuxmi.txt -o -e linuxmi.py && echo "linuxmi exists" || echo "linuxmi does not exist"

Preferred chaining:

test -e linuxmi.txt || test -e linuxmi.py && echo "linuxmi exists" || echo "linuxmi does not exist"

Using bracket syntax instead of test

The same checks can be written with [ … ], which is functionally equivalent to test:

[ -e linux.py ] && echo "linux.py exists" || echo "file1 does not exist"

Bracket form works for all the previous examples, allowing more concise multiple‑condition checks:

[ 4 -eq 4 ] && [ "moo" = "moo" ] && echo "it is a cow" || echo "it is not a cow"
[ -e linuxmi.py ] || [ -e linuxmi.txt ] && echo "linuxmi exists" || echo "linuxmi does not exist"

Summary

The test command is valuable in scripts for controlling flow based on variable values, file existence, and other conditions. On the command line it provides a quick way to verify numeric, string, or file attributes without producing output, relying on exit status for decision making.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxBashShell scriptingtest commandconditional expressions
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.