Bash Conditional Expressions and Operators

Conditional Expression Syntax

Bash supports four syntax forms for condition testing:

Syntax Description
test expression Uses the test command
[ expression ] Single brackets, functionally identical to test
[[ expression ]] Double brackets, enhanced version of the above
((expression)) Double parentheses, typically used with if statements

Key Differences

Double brackets support pattern matching with glob characters. Operators like &&, ||, <, and > work inside [[ ]] but not in single brackets. In single brackets, use -a and -o for logical operations, and replace comparison operators like -gt and -lt. Integer relational operations also work in double parentheses. Spaces are required before and after expressions in brackets.

Basic Usage

test -f /etc/passwd && echo 1 || echo 0
[ -f /etc/passwd ] && echo 1 || echo 0
[[ -f /etc/passwd ]] && echo 1 || echo 0
((3>2)) && echo 1 || echo 0

Execute only one branch by omitting the other:

test -f /etc/passwd && echo "exists"
test -f /etc/passwd || echo "missing"

Extended Syntax

For multiple commands, use the block syntax:

[ condition1 ] && {
  command1
  command2
  command3
}

File Test Operators

Operator Purpose
-d File exists and is a directory
-f File exists and is a regular file
-e File exists (any type)
-r File exists and is readable
-w File exists and is writable
-x File exists and is executable
-s File exists and has non-zero size
-L File exists and is a symbolic link
f1 -nt f2 File f1 is newer than f2
f1 -ot f2 File f1 is older than f2

These operators work with [], [[]], and test. Read, write, and execute permissions depend on the actual user context—for example, root can read files without read permission.

Examples

[ -f /etc/passwd ] && echo 1 || echo 0
[ -d /etc/passwd ] && echo 1 || echo 0
[ -e /etc/passwd ] && echo 1 || echo 0
[ -r /etc/passwd ] && echo 1 || echo 0
[ -x /etc/passwd ] && echo 1 || echo 0
[ -w /etc/passwd ] && echo 1 || echo 0
[ -L /etc/passwd ] && echo 1 || echo 0

String Test Operators

Operator Purpose
-n "string" Returns true if string length is non-zero
-z "string" Returns true if string length is zero
"str1" = "str2" Returns true if strings are equal
"str1" != "str2" Returns true if strings are not equal

Always quote strings during comparison. Comparison operators require spaces on both siddes.

Examples

[ -n "" ] && echo 1 || echo 0
[ -z "" ] && echo 1 || echo 0
[ "abc" = "abc" ] && echo 1 || echo 0
[ "abc" != "abc" ] && echo 1 || echo 0

Integer Comparison and Logical Operators

Comparison Operators

In [] and test In (()) and [[]] Meaning
-eq == or = Equal
-ne != Not equal
-gt > Greater than
-ge >= Greater than or equal
-lt < Less than
-le <= Less than or equal

The = and != operators work in [] but require escaping. While -gt and -lt function in [[]], they are not recommended.

Logical Operators

In [] and test In (()) and [[]] Meaning
-a && AND - both sides must be true
-o || OR - either side must be true
! ! NOT - negates the condition

When chaining multiple bracket expressions, use && or \|\| to connect them.

Syntax Comparison Summary

Feature [] [[]] (() )
Pattern matching No Yes No
Integer arithmetic No No Yes
&& || operators No Yes Yes
< > without escape No Yes Yes
-a -o operators Yes Yes No

Tags: bash Shell conditional-expressions Operators

Posted on Mon, 25 May 2026 19:41:41 +0000 by True`Logic