summaryrefslogtreecommitdiff
path: root/devdocs/bash/bash-conditional-expressions.html
blob: a3d13aba5dcd9a47ce76a776d9126b85b1155850 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<h1 class="section">Bash Conditional Expressions</h1>  <p>Conditional expressions are used by the <code>[[</code> compound command (see <a href="conditional-constructs">Conditional Constructs</a>) and the <code>test</code> and <code>[</code> builtin commands (see <a href="bourne-shell-builtins">Bourne Shell Builtins</a>). The <code>test</code> and <code>[</code> commands determine their behavior based on the number of arguments; see the descriptions of those commands for any other command-specific actions. </p> <p>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 <var>file</var> argument to one of the primaries is of the form <samp>/dev/fd/<var>N</var></samp>, then file descriptor <var>N</var> is checked. If the <var>file</var> argument to one of the primaries is one of <samp>/dev/stdin</samp>, <samp>/dev/stdout</samp>, or <samp>/dev/stderr</samp>, file descriptor 0, 1, or 2, respectively, is checked. </p> <p>When used with <code>[[</code>, the ‘<samp>&lt;</samp>’ and ‘<samp>&gt;</samp>’ operators sort lexicographically using the current locale. The <code>test</code> command uses ASCII ordering. </p> <p>Unless otherwise specified, primaries that operate on files follow symbolic links and operate on the target of the link, rather than the link itself. </p> <dl compact> <dt><span><code>-a <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists. </p> </dd> <dt><span><code>-b <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and is a block special file. </p> </dd> <dt><span><code>-c <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and is a character special file. </p> </dd> <dt><span><code>-d <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and is a directory. </p> </dd> <dt><span><code>-e <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists. </p> </dd> <dt><span><code>-f <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and is a regular file. </p> </dd> <dt><span><code>-g <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and its set-group-id bit is set. </p> </dd> <dt><span><code>-h <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and is a symbolic link. </p> </dd> <dt><span><code>-k <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and its "sticky" bit is set. </p> </dd> <dt><span><code>-p <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and is a named pipe (FIFO). </p> </dd> <dt><span><code>-r <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and is readable. </p> </dd> <dt><span><code>-s <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and has a size greater than zero. </p> </dd> <dt><span><code>-t <var>fd</var></code></span></dt> <dd>
<p>True if file descriptor <var>fd</var> is open and refers to a terminal. </p> </dd> <dt><span><code>-u <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and its set-user-id bit is set. </p> </dd> <dt><span><code>-w <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and is writable. </p> </dd> <dt><span><code>-x <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and is executable. </p> </dd> <dt><span><code>-G <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and is owned by the effective group id. </p> </dd> <dt><span><code>-L <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and is a symbolic link. </p> </dd> <dt><span><code>-N <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and has been modified since it was last read. </p> </dd> <dt><span><code>-O <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and is owned by the effective user id. </p> </dd> <dt><span><code>-S <var>file</var></code></span></dt> <dd>
<p>True if <var>file</var> exists and is a socket. </p> </dd> <dt><span><code><var>file1</var> -ef <var>file2</var></code></span></dt> <dd>
<p>True if <var>file1</var> and <var>file2</var> refer to the same device and inode numbers. </p> </dd> <dt><span><code><var>file1</var> -nt <var>file2</var></code></span></dt> <dd>
<p>True if <var>file1</var> is newer (according to modification date) than <var>file2</var>, or if <var>file1</var> exists and <var>file2</var> does not. </p> </dd> <dt><span><code><var>file1</var> -ot <var>file2</var></code></span></dt> <dd>
<p>True if <var>file1</var> is older than <var>file2</var>, or if <var>file2</var> exists and <var>file1</var> does not. </p> </dd> <dt><span><code>-o <var>optname</var></code></span></dt> <dd>
<p>True if the shell option <var>optname</var> is enabled. The list of options appears in the description of the <samp>-o</samp> option to the <code>set</code> builtin (see <a href="the-set-builtin">The Set Builtin</a>). </p> </dd> <dt><span><code>-v <var>varname</var></code></span></dt> <dd>
<p>True if the shell variable <var>varname</var> is set (has been assigned a value). </p> </dd> <dt><span><code>-R <var>varname</var></code></span></dt> <dd>
<p>True if the shell variable <var>varname</var> is set and is a name reference. </p> </dd> <dt><span><code>-z <var>string</var></code></span></dt> <dd>
<p>True if the length of <var>string</var> is zero. </p> </dd> <dt><span><code>-n <var>string</var></code></span></dt> <dt><span><code><var>string</var></code></span></dt> <dd>
<p>True if the length of <var>string</var> is non-zero. </p> </dd> <dt><span><code><var>string1</var> == <var>string2</var></code></span></dt> <dt><span><code><var>string1</var> = <var>string2</var></code></span></dt> <dd>
<p>True if the strings are equal. When used with the <code>[[</code> command, this performs pattern matching as described above (see <a href="conditional-constructs">Conditional Constructs</a>). </p> <p>‘<samp>=</samp>’ should be used with the <code>test</code> command for <small>POSIX</small> conformance. </p> </dd> <dt><span><code><var>string1</var> != <var>string2</var></code></span></dt> <dd>
<p>True if the strings are not equal. </p> </dd> <dt><span><code><var>string1</var> &lt; <var>string2</var></code></span></dt> <dd>
<p>True if <var>string1</var> sorts before <var>string2</var> lexicographically. </p> </dd> <dt><span><code><var>string1</var> &gt; <var>string2</var></code></span></dt> <dd>
<p>True if <var>string1</var> sorts after <var>string2</var> lexicographically. </p> </dd> <dt><span><code><var>arg1</var> OP <var>arg2</var></code></span></dt> <dd><p><code>OP</code> is one of ‘<samp>-eq</samp>’, ‘<samp>-ne</samp>’, ‘<samp>-lt</samp>’, ‘<samp>-le</samp>’, ‘<samp>-gt</samp>’, or ‘<samp>-ge</samp>’. These arithmetic binary operators return true if <var>arg1</var> is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to <var>arg2</var>, respectively. <var>Arg1</var> and <var>arg2</var> may be positive or negative integers. When used with the <code>[[</code> command, <var>Arg1</var> and <var>Arg2</var> are evaluated as arithmetic expressions (see <a href="shell-arithmetic">Shell Arithmetic</a>). </p></dd> </dl><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 2000, 2001, 2002, 2007, 2008 Free Software Foundation, Inc.<br>Licensed under the GNU Free Documentation License.<br>
    <a href="https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html" class="_attribution-link">https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html</a>
  </p>
</div>