Bash Conditional Expressions (Bash Reference Manual)
Next: Shell Arithmetic, Previous: Interactive Shells, Up: Bash Features [Contents][Index]
6.4 Bash Conditional Expressions
Conditional expressions are used by the [[../ compound command and the test and [ builtin commands. The test and [ commands determine their behavior based on the number of arguments; see the descriptions of those commands for any other command-specific actions.
Expressions may be unary or binary, and are formed from the following primaries. Unary expressions are often used to examine the status of a file. There are string operators and numeric comparison operators as well. Bash handles several filenames specially when they are used in expressions. If the operating system on which Bash is running provides these special files, Bash will use them; otherwise it will emulate them internally with this behavior: If the 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.
When used with [[, the ‘<’ and ‘>’ operators sort lexicographically using the current locale. The test command uses ASCII ordering.
Unless otherwise specified, primaries that operate on files follow symbolic links and operate on the target of the link, rather than the link itself.
-a fileTrue if
fileexists.-b fileTrue if
fileexists and is a block special file.-c fileTrue if
fileexists and is a character special file.-d fileTrue if
fileexists and is a directory.-e fileTrue if
fileexists.-f fileTrue if
fileexists and is a regular file.-g fileTrue if
fileexists and its set-group-id bit is set.-h fileTrue if
fileexists and is a symbolic link.-k fileTrue if
fileexists and its "sticky" bit is set.-p fileTrue if
fileexists and is a named pipe (FIFO).-r fileTrue if
fileexists and is readable.-s fileTrue if
fileexists and has a size greater than zero.-t fdTrue if file descriptor
fdis open and refers to a terminal.-u fileTrue if
fileexists and its set-user-id bit is set.-w fileTrue if
fileexists and is writable.-x fileTrue if
fileexists and is executable.-G fileTrue if
fileexists and is owned by the effective group id.-L fileTrue if
fileexists and is a symbolic link.-N fileTrue if
fileexists and has been modified since it was last read.-O fileTrue if
fileexists and is owned by the effective user id.-S fileTrue if
fileexists and is a socket.file1 -ef file2True if
file1andfile2refer to the same device and inode numbers.file1 -nt file2True if
file1is newer (according to modification date) thanfile2, or iffile1exists andfile2does not.file1 -ot file2True if
file1is older thanfile2, or iffile2exists andfile1does not.-o optnameTrue if the shell option
optnameis enabled. The list of options appears in the description of the-ooption to thesetbuiltin (see The Set Builtin).-v varnameTrue if the shell variable
varnameis set (has been assigned a value).-R varnameTrue if the shell variable
varnameis set and is a name reference.-z stringTrue if the length of
stringis zero.-n string
stringTrue if the length of
stringis non-zero.string1 == string2
string1 = string2True if the strings are equal. When used with the
[[../command, this performs pattern matching as described above (see Conditional Constructs).‘
=’ should be used with thetestcommand for POSIX conformance.string1 != string2True if the strings are not equal.
string1 < string2True if
string1sorts beforestring2lexicographically.string1 > string2True if
string1sorts afterstring2lexicographically.arg1 OP arg2OPis one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true ifarg1is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal toarg2, respectively.Arg1andarg2may be positive or negative integers. When used with the[[../command,Arg1andArg2are evaluated as arithmetic expressions (see Shell Arithmetic).
Next: Shell Arithmetic, Previous: Interactive Shells, Up: Bash Features [Contents][Index]