Bobby's Blog

Most popular BASH conditional expressions

Conditional expressions



In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language, which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false.

In BASH Conditional expressions are used by the [[ compound command and the test and [ builtin commands to test file attributes and perform string and arithmetic comparisons.

Expressions are formed from the following unary or binary primaries. If any file argument to one of the primaries is of the form /dev/fd/n, then file descriptor n is checked. If the file argument to one of the primaries is one of /dev/stdin, /dev/stdout, or /dev/stderr, file descriptor 0, 1, or 2, respectively, is checked.

Unless otherwise specified, primaries that operate on files follow symbolic links and operate on the target of the link, rather than the link itself.

When used with [[, the < and > operators sort lexicographically using the current locale. The test command sorts using ASCII ordering.



True if file exists.
True if file exists and is a block special file.
True if file exists and is a character special file.
True if file exists and is a directory.
True if file exists.
True if file exists and is a regular file.
True if file exists and is set-group-id.
True if file exists and is a symbolic link.
True if file exists and its sticky'' bit is set.
True if file exists and is a named pipe (FIFO).
True if file exists and is readable.
True if file exists and has a size greater than zero.
True if file descriptor fd is open and refers to a terminal.
True if file exists and its set-user-id bit is set.
True if file exists and is writable.
True if file exists and is executable.
True if file exists and is owned by the effective group id.
True if file exists and is a symbolic link.
True if file exists and has been modified since it was last read.
True if file exists and is owned by the effective user id.
True if file exists and is a socket.
True if file1 and file2 refer to the same device and inode numbers.
True if file1 is newer (according to modification date) than file2, or if file1 exists and file2 does not.
True if file1 is older than file2, or if file2 exists and file1 does not.
True if the shell option optname is enabled. See the list of options under the description of the -o option to the set builtin below.
True if the shell variable varname is set (has been assigned a value).
True if the shell variable varname is set and is a name reference.
True if the length of string is zero.
string
True if the length of string is non-zero.

True if the strings are equal. = should be used with the test command for POSIX conformance. When used with the [[ command, this performs pattern matching as described above (Compound Com‐
mands).

True if the strings are not equal.

True if string1 sorts before string2 lexicographically.

True if string1 sorts after string2 lexicographically.

OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than
or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers.


Source


About the author

Bobby

Linux System Administrator