Fundamentals 6 min read

Master Shell Logic: Essential Operators and Test Expressions Explained

This guide walks through Bash's logical operators, file‑test flags, attribute checks, comparison operators, and both [] and [[ ]] conditional expressions, showing usage examples, syntax rules, and performance benchmarks to help you write more efficient shell scripts.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Shell Logic: Essential Operators and Test Expressions Explained

1. Logical Operators

-f checks if a file exists, e.g., if [ -f filename ]. -d checks for a directory. -b tests for a block device. -c tests for a character device. -S tests for a socket. -L tests for a symbolic link. -e tests if a path exists.

Program ownership tests: -G checks if a file is owned by a group ID. -O checks if a file is owned by a user ID. -p checks for a named pipe or FIFO.

File attribute tests: -r readable, -w writable, -x executable. -s non‑empty. -u set‑uid, -g set‑gid, -k sticky bit.

File comparison operators: -nt newer than, -ot older than, -ef same file (hard link).

Logical AND/OR symbols: && (AND), || (OR).

2. Operator Symbol Meanings

String comparison inside []: = (equal), != (not equal), < (less than), > (greater than). Integer comparison uses -eq, -ne, -lt, -gt, -le, -ge.

Combined tests: -a (AND), -o (OR), -z (empty string), -n (non‑empty string).

3. Logical Expressions

1. test command

Usage: test EXPRESSION. Example:

test 1 = 1 && echo 'ok'
# prints ok

Another example: test -d /etc/ && echo 'ok' Note: operators and operands must be separated by spaces.

2. Simplified [] expression

Example: [ 1 -eq 1 ] && echo 'ok' When using < or > inside [], they must be escaped because they are interpreted as redirection.

Logical AND/OR inside [] are expressed with -a and -o respectively.

3. [[]] expression

Example:

[[ 2 < 3 ]] && echo 'ok'
[[ 2 < 3 && 4 > 5 ]] && echo 'ok'
[[ ]]

supports < and > without escaping and allows logical operators || and &&.

Performance comparison

Benchmarks (100 000 iterations): time (for m in {1..100000}; do test -d .; done) real 0m0.658s, user 0m0.558s, sys 0m0.100s time (for m in {1..100000}; do [ -d . ]; done) real 0m0.609s, user 0m0.524s, sys 0m0.085s

time (for m in {1..100000}; do [[ -d . ]]; done)

real 0m0.311s, user 0m0.275s, sys 0m0.036s

Conclusion: [[ ]] is the fastest and, when compatibility with modern Bash is acceptable, is the preferred choice for conditional tests.

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.

Bashlogical operatorstest commandconditional expressions
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.