Fundamentals 8 min read

Mastering Bash Logical Operators: File Tests, Comparisons, and Performance Tips

This guide explains Bash logical test operators, file‑checking flags, comparison symbols, bracket expressions, and shows a performance benchmark where the [[ ]] syntax outperforms test and [] in large loops.

ITPUB
ITPUB
ITPUB
Mastering Bash Logical Operators: File Tests, Comparisons, and Performance Tips

Logical Test Operators

-f

– checks if a file exists (e.g., if [ -f filename ]) -d – checks if a directory exists -b – checks for a block device -c – checks for a character device -S – checks for a socket -L – checks for a symbolic link -e – checks if a path exists (file, directory, etc.) -G – true if the file’s group ID matches the effective GID -O – true if the file’s owner ID matches the effective UID -p – checks for a named pipe (FIFO) -r – readable -w – writable -x – executable -s – non‑empty file -u – set‑uid bit set -g – set‑gid bit set -k – sticky bit set

File Comparison Operators

-nt

– first file is newer than the second -ot – first file is older than the second -ef – both names refer to the same inode (hard link)

Logical Connectors

&&

– logical AND || – logical OR -a – AND inside a single [ ] test expression -o – OR inside a single [ ] test expression -z – string is empty -n – string is non‑empty

Using the test Command

Syntax: test EXPRESSION. Examples:

# Simple equality
[ root@localhost ~ ]# test 1 = 1 && echo 'ok'
ok

# Directory existence
[ root@localhost ~ ]# test -d /etc && echo 'ok'
ok

# Numeric comparison
[ root@localhost ~ ]# test 1 -eq 1 && echo 'ok'
ok

When using if:

if test 1 = 1; then echo 'ok'; fi
ok

All tokens and operators must be separated by spaces; they cannot be concatenated.

Bracket Expressions

Single brackets [ ] are the traditional test syntax. Comparison operators such as < and > must be escaped because they are interpreted as redirection symbols. Logical operators || and && are not recognized; use -a and -o instead.

Double brackets [[ ]] extend [ ] capabilities: they allow unescaped < and > for string comparison and support || and && directly.

# Using []
[ root@localhost ~ ]# [ 2 -gt 1 -a 3 -lt 4 ] && echo 'ok'
ok

# Using [[ ]]
[ root@localhost ~ ]$ [[ 2 < 3 && 4 > 5 ]] && echo 'ok'
ok

Performance Comparison

Three forms— test, [ ], and [[ ]] —were timed in a loop of 100 000 iterations checking the current directory.

# test
real 0m0.658s  user 0m0.558s  sys 0m0.100s

# [ ]
real 0m0.609s  user 0m0.524s  sys 0m0.085s

# [[ ]]
real 0m0.311s  user 0m0.275s  sys 0m0.036s

When compatibility with older Bash or POSIX sh is not required, [[ ]] offers the best performance and broader feature set for conditional expressions.

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.

ScriptingBashlogical operatorstest command
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.