Search⌘ K

Advanced Features of the [[ Operator

Explore advanced features of the Bash [[ operator to perform logical AND, OR, and NOT operations, group expressions, and check file and directory states. Understand how to create complex conditional statements that help automate tasks and manage files effectively in your scripts.

Logical operations in the [[ operator

We can use logical operations AND, OR, and NOT in the [[ operator. They combine several boolean expressions into a single condition. The following table explains how they work.

Operation Description Example
&& Logical AND [[ -n "$var" && "$var" < "abc" ]] && echo "The string is not empty and it is smaller than \"abc\""
|| Logical OR [[ "abc" < "$var" \|\| -z "$var" ]] && echo "The string is larger than \"abc\" or it is empty"
! Logical NOT [[ ! "abc" <
...