Bash Conditional Operators Guide (-? Options)

Bash conditional operators guide #bash #conditionaloperators

This document provides an overview of common bash conditional operators (unary test options) used in bash within [[ ... ]] or [ ... ] expressions.

String Tests

OperatorDescriptionExample
-zTests if the string is empty (zero length).[[ -z "$var" ]] – True if $var is empty.
-nTests if the string is non-empty.[[ -n "$var" ]] – True if $var is non-empty.
==String comparison for equality. Bash-specific.[[ "$var1" == "$var2" ]] – True if $var1 equals $var2.
!=String comparison for inequality.[[ "$var1" != "$var2" ]] – True if $var1 is not equal to $var2.

File Tests

OperatorDescriptionExample
-eChecks if the file or directory exists.[[ -e /path/to/file ]] – True if the file exists.
-fChecks if the path is a regular file.[[ -f /path/to/file ]] – True if it is a regular file.
-dChecks if the path is a directory.[[ -d /path/to/dir ]] – True if it is a directory.
-rChecks if the file is readable.[[ -r /path/to/file ]] – True if the file is readable.
-wChecks if the file is writable.[[ -w /path/to/file ]] – True if the file is writable.
-xChecks if the file is executable.[[ -x /path/to/file ]] – True if the file is executable.
-sChecks if the file has size greater than zero.[[ -s /path/to/file ]] – True if the file has non-zero size.
-LChecks if the path is a symbolic link.[[ -L /path/to/link ]] – True if it is a symlink.
-pChecks if the file is a named pipe (FIFO).[[ -p /path/to/pipe ]] – True if it is a named pipe.
-SChecks if the file is a socket.[[ -S /path/to/socket ]] – True if it is a socket.
-bChecks if the file is a block device.[[ -b /path/to/device ]] – True if it is a block device.
-cChecks if the file is a character device.[[ -c /path/to/device ]] – True if it is a character device.

Permissions and Ownership Tests

OperatorDescriptionExample
-uChecks if the file has the setuid bit set.[[ -u /path/to/file ]] – True if setuid is set.
-gChecks if the file has the setgid bit set.[[ -g /path/to/file ]] – True if setgid is set.
-kChecks if the file has the sticky bit set.[[ -k /path/to/file ]] – True if the sticky bit is set.
-OChecks if the current user owns the file.[[ -O /path/to/file ]] – True if owned by the current user.
-GChecks if the file’s group matches the user’s group.[[ -G /path/to/file ]] – True if group matches.

Integer Comparisons

OperatorDescriptionExample
-eqTests if two integers are equal.[[ "$num1" -eq "$num2" ]] – True if $num1 equals $num2.
-neTests if two integers are not equal.[[ "$num1" -ne "$num2" ]] – True if $num1 is not equal to $num2.
-ltTests if one integer is less than another.[[ "$num1" -lt "$num2" ]] – True if $num1 is less than $num2.
-leTests if one integer is less than or equal to another.[[ "$num1" -le "$num2" ]] – True if $num1 is less than or equal to $num2.
-gtTests if one integer is greater than another.[[ "$num1" -gt "$num2" ]] – True if $num1 is greater than $num2.
-geTests if one integer is greater than or equal to another.[[ "$num1" -ge "$num2" ]] – True if $num1 is greater than or equal to $num2.

Miscellaneous

OperatorDescriptionExample
-tTests if the file descriptor is open and refers to a terminal.[[ -t 1 ]] – True if file descriptor 1 is a terminal.
-vTests if a variable is set (bash-specific).[[ -v varname ]] – True if varname is set.
-RTests if a file is readable by the real user (instead of the effective user).[[ -R /path/to/file ]] – True if readable by real user.

Combining Conditions

You can combine multiple conditions using logical operators:

OperatorDescriptionExample
&&Logical AND.[[ -n "$var" && -f "$file" ]] – True if $var is non-empty AND $file exists.
``
!Logical NOT.[[ ! -f "$file" ]] – True if $file does NOT exist.

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *