Fundamentals 5 min read

Master Bash Test Commands: Conditional Logic, Comparisons, and File Checks

Learn how Bash’s test command, brackets, arithmetic expressions, and if/then constructs enable condition testing, numeric, string, and file comparisons, with clear examples and syntax for practical shell scripting, including block and character device checks and common operators.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Bash Test Commands: Conditional Logic, Comparisons, and File Checks

Shell test command

Every reasonable programming language can test a condition and act on the result. Bash provides the test builtin, the [ ] and [[ ]] operators, arithmetic (( )) and the if … then … fi construct.

Related commands and symbols

If/then conditional statements.

Built‑in [ ] and enhanced [[ ]] brackets.

Arithmetic (( )) and let for numeric expressions.

Checking any command with if/then

#!/bin/bash
if cmp a.txt b.txt > /dev/null
then
    echo 'Files a and b are identical.'
else
    echo 'Files a and b are diff.'
fi

if/then syntax examples

if [ condition-true ]
then
    command1
    command2
    ...
else   # or elif …
    command3
    command4
    ...
fi
if [ condition1 ]
then
    command1
    command2
    command3
elif [ condition2 ]
then
    command4
    command5
else
    default-command
fi

Arithmetic tests with (( ))

#!/bin/bash
var1=7
var2=10
if (( var1 > var2 ))
then
    echo "var1 is greater than var2"
else
    echo "var1 is less than var2"
fi
exit 0

Numeric comparison operators

-eq

: equal, -ne: not equal, -gt: greater than, -ge: greater or equal, -lt: less than, -le: less or equal.

String comparison operators

str1 = str2

: equal, str1 != str2: not equal, -z str1: zero‑length, -n str1: non‑zero length, str1 > str2: lexicographically greater, str1 < str2: lexicographically less.

File test operators

-e file

: exists, -r file: readable, -w file: writable, -x file: executable, -s file: non‑empty, -d file: directory, -f file: regular file, -c file: character device, -b file: block device.

Examples of file type tests

#!/bin/bash
device1="/dev/sda1"
if [ -b "$device1" ]; then
    echo "$device1 is a block device."
fi

device2='/dev/tty0'
if [ -c "$device2" ]; then
    echo "$device2 is a character device."
fi
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.

BashShell scriptingconditional statementstest commandfile tests
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.