Master Shell Logical Operators to Supercharge Your Bash Scripts
This guide explains Bash logical operators, file‑test flags, comparison symbols, and conditional expressions, showing how to choose the right tests, use test, [] and [[]] syntax, and improve script performance with practical examples.
1. Logical Operators
File and directory test flags -f – check if a file exists -d – check if a directory exists -b – check for a block device -c – check for a character device -S – check for a socket -L – check for a symbolic link -e – check if something exists
Program‑related flags -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 – true if the file is a named pipe (FIFO)
File attribute flags -r – readable -w – writable -x – executable -s – non‑empty -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 file
Logical AND / OR && – logical AND || – logical OR
2. Comparison Symbols
=– equal (string comparison inside []) != – not equal (string comparison inside []) < – less than (integer comparison; inside [] it must be a string) > – greater than (integer comparison; same restriction) -eq – equal (integer) -ne – not equal (integer) -lt – less than (integer) -gt – greater than (integer) -le – less than or equal (integer) -ge – greater than or equal (integer) -a – logical AND (old syntax) -o – logical OR (old syntax) -z – string is empty -n – string is non‑empty
3. Conditional Expressions
test command
Usage: test EXPRESSION Examples:
[root@localhost ~]# test 1 = 1 && echo 'ok'
ok
[root@localhost ~]# test -d /etc/ && echo 'ok'
ok
[root@localhost ~]# test 1 -eq 1 && echo 'ok'
ok
[root@localhost ~]# if test 1 = 1 ; then echo 'ok'; fi
okNote: All operators and operands must be separated by spaces.
Bracket expressions
Single brackets []:
[root@localhost ~]# [ 1 -eq 1 ] && echo 'ok'
ok
[root@localhost ~]# [ 2 < 1 ] && echo 'ok'
-bash: 2: No such file or directory
[root@localhost ~]# [ 2 -gt 1 -a 3 -lt 4 ] && echo 'ok'
okDouble brackets [[]] support < and > without escaping and allow logical operators || and &&:
[root@localhost ~]$ [[ 2 < 3 ]] && echo 'ok'
ok
[root@localhost ~]$ [[ 2 < 3 && 4 > 5 ]] && echo 'ok'
okPerformance comparison (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.036sWhen compatibility with older Bash or POSIX sh is not required, [[]] offers stronger compatibility and better performance for conditional checks.
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.
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.
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.
