Master Bash Conditional Logic: Essential Operators and Best Practices
This guide explains Bash logical operators, file-test flags, comparison symbols, and expression syntax—including test, [] and [[]]—with practical examples and performance tips to help you write efficient and reliable shell conditionals.
1. Logical Operators
Shell provides a variety of test flags that can be used to evaluate files, directories, and other attributes, enabling concise and powerful conditional statements. -f – checks if a file exists (e.g., if [ -f filename ]) -d – checks if a directory exists -b – checks for a block device file -c – checks for a character device file -S – checks for a socket file -L – checks for a symbolic link -e – checks if a path exists -G – true if the file is owned by the effective group ID -O – true if the file is owned by the effective user ID -p – checks for a named pipe (FIFO) -r – checks if the file is readable -w – checks if the file is writable -x – checks if the file is executable -s – checks if the file size is greater than zero (non‑empty) -u – checks for the SUID bit -g – checks for the SGID bit -k – checks for the sticky bit
File‑to‑file comparisons include: -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 conjunction and disjunction can be expressed with && (AND) and || (OR).
2. Operator Symbols
When using test or [ ], the following symbols are common: = – string equality != – string inequality < – string less‑than (requires escaping in [ ]) > – string greater‑than (requires escaping in [ ]) -eq – numeric equality -ne – numeric inequality -lt – numeric less‑than -gt – numeric greater‑than -le – numeric less‑or‑equal -ge – numeric greater‑or‑equal -a – logical AND (alternative syntax) -o – logical OR (alternative syntax) -z – tests for an empty string -n – tests for a non‑empty string
3. Logical 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.
Simplified expressions [] expression:
[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'
okWhen using [], the characters < and > must be escaped because they are interpreted as redirection operators.
[[]] expression
[root@localhost ~]$ [[ 2 < 3 ]] && echo 'ok'
ok [root@localhost ~]$ [[ 2 < 3 && 4 > 5 ]] && echo 'ok'
okThe [[ ]] construct extends [] by allowing unescaped < and > for string comparison and supports logical operators || and && directly.
Performance comparison
Three common conditional forms were timed over 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 versions or POSIX sh is not required, [[ ]] offers the best performance and flexibility 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.
