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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
<!DOCTYPE html>
<html lang="en" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>test - perform tests on files and text — fish-shell 4.2.1 documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=4da8bbd6" />
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=f89b4716" />
<script src="../_static/documentation_options.js?v=6fb65176"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="time - measure how long a command or block takes" href="time.html" />
<link rel="prev" title="switch - conditionally execute a block of commands" href="switch.html" />
<link rel="shortcut icon" type="image/png" href="../_static/fish.png" />
</head><body><div id="fmain">
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li><img src="../_static/fish.png" alt=""
style="width: 80px; height: 80px; vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://fishshell.com/">fish-shell</a> »</li>
<a href="../index.html">fish-shell 4.2.1 documentation</a> »
<li class="nav-item nav-item-1"><a href="../commands.html" accesskey="U">Commands</a> »</li>
<li class="nav-item nav-item-this"><a href="">test - perform tests on files and text</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</li>
<div id="old-docs-notice" style="display: none">
This documents an old version of fish.
<a href="../../current/">See the latest release.</a>
</div>
</ul>
</div>
<div class="document">
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<div>
<h3><a href="../index.html">Documents</a></h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../index.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">Frequently asked questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../interactive.html">Interactive use</a></li>
<li class="toctree-l1"><a class="reference internal" href="../language.html">The fish language</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../commands.html">Commands</a></li>
<li class="toctree-l1"><a class="reference internal" href="../fish_for_bash_users.html">Fish for bash users</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../completions.html">Writing your own completions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../prompt.html">Writing your own prompt</a></li>
<li class="toctree-l1"><a class="reference internal" href="../design.html">Design</a></li>
<li class="toctree-l1"><a class="reference internal" href="../relnotes.html">Release notes</a></li>
<li class="toctree-l1"><a class="reference internal" href="../terminal-compatibility.html">Terminal Compatibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributing.html">Contributing To Fish</a></li>
<li class="toctree-l1"><a class="reference internal" href="../license.html">License</a></li>
</ul>
</div>
<search id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
<div>
<h4><a href="../index.html">Sections</a></h4>
<ul>
<li><a class="reference internal" href="#">test - perform tests on files and text</a><ul>
<li><a class="reference internal" href="#synopsis">Synopsis</a></li>
<li><a class="reference internal" href="#description">Description</a></li>
<li><a class="reference internal" href="#operators-for-files-and-directories">Operators for files and directories</a></li>
<li><a class="reference internal" href="#operators-to-compare-files-and-directories">Operators to compare files and directories</a></li>
<li><a class="reference internal" href="#operators-for-text-strings">Operators for text strings</a></li>
<li><a class="reference internal" href="#operators-to-compare-and-examine-numbers">Operators to compare and examine numbers</a></li>
<li><a class="reference internal" href="#operators-to-combine-expressions">Operators to combine expressions</a></li>
<li><a class="reference internal" href="#examples">Examples</a></li>
<li><a class="reference internal" href="#standards">Standards</a></li>
<li><a class="reference internal" href="#see-also">See also</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="test-perform-tests-on-files-and-text">
<h1>test - perform tests on files and text<a class="headerlink" href="#test-perform-tests-on-files-and-text" title="Link to this heading">¶</a></h1>
<section id="synopsis">
<h2>Synopsis<a class="headerlink" href="#synopsis" title="Link to this heading">¶</a></h2>
<div class="highlight-fish-synopsis notranslate"><div class="highlight"><pre><span></span><span class="nf">test</span><span class="w"> </span><span class="g g-Other">[</span><span class="no">EXPRESSION</span><span class="g g-Other">]</span>
<span class="nf">[ </span><span class="g g-Other">[</span><span class="no">EXPRESSION</span><span class="g g-Other">]</span><span class="no"> ]</span>
</pre></div>
</div>
</section>
<section id="description">
<h2>Description<a class="headerlink" href="#description" title="Link to this heading">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">test</span></code> checks the given conditions and sets the exit status to 0 if they are true, 1 if they are false.</p>
<p>The first form (<code class="docutils literal notranslate"><span class="pre">test</span></code>) is preferred. For compatibility with other shells, the second form is available: a matching pair of square brackets (<code class="docutils literal notranslate"><span class="pre">[</span> <span class="pre">[EXPRESSION]</span> <span class="pre">]</span></code>).</p>
<p>When using a variable or command substitution as an argument with <code class="docutils literal notranslate"><span class="pre">test</span></code> you should almost always enclose it in double-quotes, as variables expanding to zero or more than one argument will most likely interact badly with <code class="docutils literal notranslate"><span class="pre">test</span></code>.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>For historical reasons, <code class="docutils literal notranslate"><span class="pre">test</span></code> supports the one-argument form (<code class="docutils literal notranslate"><span class="pre">test</span> <span class="pre">foo</span></code>), and this will also be triggered by e.g. <code class="docutils literal notranslate"><span class="pre">test</span> <span class="pre">-n</span> <span class="pre">$foo</span></code> if $foo is unset. We recommend you don’t use the one-argument form and quote all variables or command substitutions used with <code class="docutils literal notranslate"><span class="pre">test</span></code>.</p>
<p>This confusing misfeature will be removed in future. <code class="docutils literal notranslate"><span class="pre">test</span> <span class="pre">-n</span></code> without any additional argument will be false, <code class="docutils literal notranslate"><span class="pre">test</span> <span class="pre">-z</span></code> will be true and any other invocation with exactly one or zero arguments, including <code class="docutils literal notranslate"><span class="pre">test</span> <span class="pre">-d</span></code> and <code class="docutils literal notranslate"><span class="pre">test</span> <span class="pre">"foo"</span></code> will be an error.</p>
<p>The same goes for <code class="docutils literal notranslate"><span class="pre">[</span></code>, e.g. <code class="docutils literal notranslate"><span class="pre">[</span> <span class="pre">"foo"</span> <span class="pre">]</span></code> and <code class="docutils literal notranslate"><span class="pre">[</span> <span class="pre">-d</span> <span class="pre">]</span></code> will be errors.</p>
<p>This can be turned on already via the <code class="docutils literal notranslate"><span class="pre">test-require-arg</span></code> <a class="reference internal" href="../language.html#featureflags"><span class="std std-ref">feature flag</span></a>, and will eventually become the default and then only option.</p>
</div>
</section>
<section id="operators-for-files-and-directories">
<h2>Operators for files and directories<a class="headerlink" href="#operators-for-files-and-directories" title="Link to this heading">¶</a></h2>
<dl class="simple">
<dt><strong>-b</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> is a block device.</p>
</dd>
<dt><strong>-c</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> is a character device.</p>
</dd>
<dt><strong>-d</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> is a directory.</p>
</dd>
<dt><strong>-e</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> exists.</p>
</dd>
<dt><strong>-f</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> is a regular file.</p>
</dd>
<dt><strong>-g</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> has the set-group-ID bit set.</p>
</dd>
<dt><strong>-G</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> exists and has the same group ID as the current user.</p>
</dd>
<dt><strong>-k</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> has the sticky bit set. If the OS does not support the concept it returns false. See <a class="reference external" href="https://en.wikipedia.org/wiki/Sticky_bit">https://en.wikipedia.org/wiki/Sticky_bit</a>.</p>
</dd>
<dt><strong>-L</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> is a symbolic link.</p>
</dd>
<dt><strong>-O</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> exists and is owned by the current user.</p>
</dd>
<dt><strong>-p</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> is a named pipe.</p>
</dd>
<dt><strong>-r</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> is marked as readable.</p>
</dd>
<dt><strong>-s</strong> <em>FILE</em></dt><dd><p>Returns true if the size of <em>FILE</em> is greater than zero.</p>
</dd>
<dt><strong>-S</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> is a socket.</p>
</dd>
<dt><strong>-t</strong> <em>FD</em></dt><dd><p>Returns true if the file descriptor <em>FD</em> is a terminal (TTY).</p>
</dd>
<dt><strong>-u</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> has the set-user-ID bit set.</p>
</dd>
<dt><strong>-w</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> is marked as writable; note that this does not check if the filesystem is read-only.</p>
</dd>
<dt><strong>-x</strong> <em>FILE</em></dt><dd><p>Returns true if <em>FILE</em> is marked as executable.</p>
</dd>
</dl>
</section>
<section id="operators-to-compare-files-and-directories">
<h2>Operators to compare files and directories<a class="headerlink" href="#operators-to-compare-files-and-directories" title="Link to this heading">¶</a></h2>
<dl class="simple">
<dt><em>FILE1</em> <strong>-nt</strong> <em>FILE2</em></dt><dd><p>Returns true if <em>FILE1</em> is newer than <em>FILE2</em>, or <em>FILE1</em> exists and <em>FILE2</em> does not.</p>
</dd>
<dt><em>FILE1</em> <strong>-ot</strong> <em>FILE2</em></dt><dd><p>Returns true if <em>FILE1</em> is older than <em>FILE2</em>, or <em>FILE2</em> exists and <em>FILE1</em> does not.</p>
</dd>
<dt><em>FILE1</em> <strong>-ef</strong> <em>FILE1</em></dt><dd><p>Returns true if <em>FILE1</em> and <em>FILE2</em> refer to the same file.</p>
</dd>
</dl>
</section>
<section id="operators-for-text-strings">
<h2>Operators for text strings<a class="headerlink" href="#operators-for-text-strings" title="Link to this heading">¶</a></h2>
<dl class="simple">
<dt><em>STRING1</em> <strong>=</strong> <em>STRING2</em></dt><dd><p>Returns true if the strings <em>STRING1</em> and <em>STRING2</em> are identical.</p>
</dd>
<dt><em>STRING1</em> <strong>!=</strong> <em>STRING2</em></dt><dd><p>Returns true if the strings <em>STRING1</em> and <em>STRING2</em> are not identical.</p>
</dd>
<dt><strong>-n</strong> <em>STRING</em></dt><dd><p>Returns true if the length of <em>STRING</em> is non-zero.</p>
</dd>
<dt><strong>-z</strong> <em>STRING</em></dt><dd><p>Returns true if the length of <em>STRING</em> is zero.</p>
</dd>
</dl>
</section>
<section id="operators-to-compare-and-examine-numbers">
<h2>Operators to compare and examine numbers<a class="headerlink" href="#operators-to-compare-and-examine-numbers" title="Link to this heading">¶</a></h2>
<dl class="simple">
<dt><em>NUM1</em> <strong>-eq</strong> <em>NUM2</em></dt><dd><p>Returns true if <em>NUM1</em> and <em>NUM2</em> are numerically equal.</p>
</dd>
<dt><em>NUM1</em> <strong>-ne</strong> <em>NUM2</em></dt><dd><p>Returns true if <em>NUM1</em> and <em>NUM2</em> are not numerically equal.</p>
</dd>
<dt><em>NUM1</em> <strong>-gt</strong> <em>NUM2</em></dt><dd><p>Returns true if <em>NUM1</em> is greater than <em>NUM2</em>.</p>
</dd>
<dt><em>NUM1</em> <strong>-ge</strong> <em>NUM2</em></dt><dd><p>Returns true if <em>NUM1</em> is greater than or equal to <em>NUM2</em>.</p>
</dd>
<dt><em>NUM1</em> <strong>-lt</strong> <em>NUM2</em></dt><dd><p>Returns true if <em>NUM1</em> is less than <em>NUM2</em>.</p>
</dd>
<dt><em>NUM1</em> <strong>-le</strong> <em>NUM2</em></dt><dd><p>Returns true if <em>NUM1</em> is less than or equal to <em>NUM2</em>.</p>
</dd>
</dl>
<p>Both integers and floating point numbers are supported.</p>
</section>
<section id="operators-to-combine-expressions">
<h2>Operators to combine expressions<a class="headerlink" href="#operators-to-combine-expressions" title="Link to this heading">¶</a></h2>
<dl class="simple">
<dt><em>COND1</em> <strong>-a</strong> <em>COND2</em></dt><dd><p>Returns true if both <em>COND1</em> and <em>COND2</em> are true.</p>
</dd>
<dt><em>COND1</em> <strong>-o</strong> <em>COND2</em></dt><dd><p>Returns true if either <em>COND1</em> or <em>COND2</em> are true.</p>
</dd>
</dl>
<p>Expressions can be inverted using the <strong>!</strong> operator:</p>
<dl class="simple">
<dt><strong>!</strong> <em>EXPRESSION</em></dt><dd><p>Returns true if <em>EXPRESSION</em> is false, and false if <em>EXPRESSION</em> is true.</p>
</dd>
</dl>
<p>Expressions can be grouped using parentheses.</p>
<dl class="simple">
<dt><strong>(</strong> <em>EXPRESSION</em> <strong>)</strong></dt><dd><p>Returns the value of <em>EXPRESSION</em>.</p>
</dd>
</dl>
<p>Note that parentheses will usually require escaping with <code class="docutils literal notranslate"><span class="pre">\</span></code> (so they appear as <code class="docutils literal notranslate"><span class="pre">\(</span></code> and <code class="docutils literal notranslate"><span class="pre">\)</span></code>) to avoid being interpreted as a command substitution.</p>
</section>
<section id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
<p>If the <code class="docutils literal notranslate"><span class="pre">/tmp</span></code> directory exists, copy the <code class="docutils literal notranslate"><span class="pre">/etc/motd</span></code> file to it:</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">if</span><span class="w"> </span><span class="nf">test</span><span class="w"> </span><span class="no">-d</span><span class="w"> </span><span class="no">/tmp</span>
<span class="w"> </span><span class="nf">cp</span><span class="w"> </span><span class="no">/etc/motd</span><span class="w"> </span><span class="no">/tmp/motd</span>
<span class="nf">end</span>
</pre></div>
</div>
<p>If the variable <span class="target" id="index-0"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">MANPATH</span></code> is defined and not empty, print the contents. (If <span class="target" id="index-1"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">MANPATH</span></code> is not defined, then it will expand to zero arguments, unless quoted.)</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">if</span><span class="w"> </span><span class="nf">test</span><span class="w"> </span><span class="no">-n</span><span class="w"> </span><span class="s2">"</span><span class="o">$MANPATH</span><span class="s2">"</span>
<span class="w"> </span><span class="nf">echo</span><span class="w"> </span><span class="o">$MANPATH</span>
<span class="nf">end</span>
</pre></div>
</div>
<p>Be careful with unquoted variables:</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">if</span><span class="w"> </span><span class="nf">test</span><span class="w"> </span><span class="no">-n</span><span class="w"> </span><span class="o">$MANPATH</span>
<span class="w"> </span><span class="c"># This will also be reached if $MANPATH is unset,</span>
<span class="w"> </span><span class="c"># because in that case we have `test -n`, so it checks if "-n" is non-empty, and it is.</span>
<span class="w"> </span><span class="nf">echo</span><span class="w"> </span><span class="o">$MANPATH</span>
<span class="nf">end</span>
</pre></div>
</div>
<p>This will change in a future release of fish, or already with the <code class="docutils literal notranslate"><span class="pre">test-require-arg</span></code> <a class="reference internal" href="../language.html#featureflags"><span class="std std-ref">feature flag</span></a> - if $MANPATH is unset, <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">test</span> <span class="pre">-n</span> <span class="pre">$MANPATH</span></code> will be false.</p>
<p>Parentheses and the <code class="docutils literal notranslate"><span class="pre">-o</span></code> and <code class="docutils literal notranslate"><span class="pre">-a</span></code> operators can be combined to produce more complicated expressions. In this example, success is printed if there is a <code class="docutils literal notranslate"><span class="pre">/foo</span></code> or <code class="docutils literal notranslate"><span class="pre">/bar</span></code> file as well as a <code class="docutils literal notranslate"><span class="pre">/baz</span></code> or <code class="docutils literal notranslate"><span class="pre">/bat</span></code> file.</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">if</span><span class="w"> </span><span class="nf">test</span><span class="w"> </span><span class="se">\(</span><span class="w"> </span><span class="no">-f</span><span class="w"> </span><span class="no">/foo</span><span class="w"> </span><span class="no">-o</span><span class="w"> </span><span class="no">-f</span><span class="w"> </span><span class="no">/bar</span><span class="w"> </span><span class="se">\)</span><span class="w"> </span><span class="no">-a</span><span class="w"> </span><span class="se">\(</span><span class="w"> </span><span class="no">-f</span><span class="w"> </span><span class="no">/baz</span><span class="w"> </span><span class="no">-o</span><span class="w"> </span><span class="no">-f</span><span class="w"> </span><span class="no">/bat</span><span class="w"> </span><span class="se">\)</span>
<span class="w"> </span><span class="nf">echo</span><span class="w"> </span><span class="no">Success.</span>
<span class="nf">end</span>
</pre></div>
</div>
<p>Numerical comparisons will fail if one of the operands is not a number:</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">if</span><span class="w"> </span><span class="nf">test</span><span class="w"> </span><span class="no">42</span><span class="w"> </span><span class="no">-eq</span><span class="w"> </span><span class="s2">"The answer to life, the universe and everything"</span>
<span class="w"> </span><span class="nf">echo</span><span class="w"> </span><span class="no">So</span><span class="w"> </span><span class="no">long</span><span class="w"> </span><span class="no">and</span><span class="w"> </span><span class="no">thanks</span><span class="w"> </span><span class="no">for</span><span class="w"> </span><span class="no">all</span><span class="w"> </span><span class="no">the</span><span class="w"> </span><span class="no">fish</span><span class="w"> </span><span class="c"># will not be executed</span>
<span class="nf">end</span>
</pre></div>
</div>
<p>A common comparison is with <span class="target" id="index-2"></span><a class="reference internal" href="../language.html#envvar-status"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">status</span></code></a>:</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">if</span><span class="w"> </span><span class="nf">test</span><span class="w"> </span><span class="o">$status</span><span class="w"> </span><span class="no">-eq</span><span class="w"> </span><span class="no">0</span>
<span class="w"> </span><span class="nf">echo</span><span class="w"> </span><span class="s2">"Previous command succeeded"</span>
<span class="nf">end</span>
</pre></div>
</div>
<p>The previous test can likewise be inverted:</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">if</span><span class="w"> </span><span class="nf">test</span><span class="w"> </span><span class="no">!</span><span class="w"> </span><span class="o">$status</span><span class="w"> </span><span class="no">-eq</span><span class="w"> </span><span class="no">0</span>
<span class="w"> </span><span class="nf">echo</span><span class="w"> </span><span class="s2">"Previous command failed"</span>
<span class="nf">end</span>
</pre></div>
</div>
<p>which is logically equivalent to the following:</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">if</span><span class="w"> </span><span class="nf">test</span><span class="w"> </span><span class="o">$status</span><span class="w"> </span><span class="no">-ne</span><span class="w"> </span><span class="no">0</span>
<span class="w"> </span><span class="nf">echo</span><span class="w"> </span><span class="s2">"Previous command failed"</span>
<span class="nf">end</span>
</pre></div>
</div>
</section>
<section id="standards">
<h2>Standards<a class="headerlink" href="#standards" title="Link to this heading">¶</a></h2>
<p>Unlike many things in fish, <code class="docutils literal notranslate"><span class="pre">test</span></code> implements a subset of the <a class="reference external" href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html">IEEE Std 1003.1-2008 (POSIX.1) standard</a>. The following exceptions apply:</p>
<ul class="simple">
<li><p>The <code class="docutils literal notranslate"><span class="pre"><</span></code> and <code class="docutils literal notranslate"><span class="pre">></span></code> operators for comparing strings are not implemented.</p></li>
<li><p>With <code class="docutils literal notranslate"><span class="pre">test-require-arg</span></code>, the zero- and one-argument modes will behave differently.</p></li>
</ul>
<blockquote>
<div><p>In cases such as this, one can use <code class="docutils literal notranslate"><span class="pre">command</span></code> <code class="docutils literal notranslate"><span class="pre">test</span></code> to explicitly use the system’s standalone <code class="docutils literal notranslate"><span class="pre">test</span></code> rather than this <code class="docutils literal notranslate"><span class="pre">builtin</span></code> <code class="docutils literal notranslate"><span class="pre">test</span></code>.</p>
</div></blockquote>
</section>
<section id="see-also">
<h2>See also<a class="headerlink" href="#see-also" title="Link to this heading">¶</a></h2>
<p>Other commands that may be useful as a condition, and are often easier to use:</p>
<ul class="simple">
<li><p><a class="reference internal" href="string.html"><span class="doc">string - manipulate strings</span></a>, which can do string operations including wildcard and regular expression matching</p></li>
<li><p><a class="reference internal" href="path.html"><span class="doc">path - manipulate and check paths</span></a>, which can do file checks and operations, including filters on multiple paths at once</p></li>
</ul>
</section>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li><img src="../_static/fish.png" alt=""
style="width: 80px; height: 80px; vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://fishshell.com/">fish-shell</a> »</li>
<a href="../index.html">fish-shell 4.2.1 documentation</a> »
<li class="nav-item nav-item-1"><a href="../commands.html" >Commands</a> »</li>
<li class="nav-item nav-item-this"><a href="">test - perform tests on files and text</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</li>
<div id="old-docs-notice" style="display: none">
This documents an old version of fish.
<a href="../../current/">See the latest release.</a>
</div>
</ul>
</div>
<div class="footer">
© Copyright fish-shell developers.
<br />
<a href="https://github.com/fish-shell/fish-shell/issues">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.
</div>
</div>
<script type="text/javascript">
FISH_DOCS_VERSION = "4.2";
function copy_to_clipboard(it) {
// Find the pre tag we're interested in.
var pre = it.target;
while (pre.tagName != "PRE") pre = pre.parentNode;
var txt = "";
// Cheesy: If we have a prompt,
// we only copy prompted lines,
// by splitting and matching and stuff
if (pre.querySelector('span.gp')) {
var texts= [];
for (var line of pre.innerText.split('\n')) {
if (line.match(/^>_?.*/)) {
texts.push(line.replace(/^>_?/, ""));
}
}
txt = texts.join("\n");
} else {
// Even cheesier: If we don't have a prompt, we remove the button text from the end.
var txt = pre.innerText.substring(0, pre.innerText.length - it.target.innerText.length).trim();
}
navigator.clipboard.writeText(txt).then(function() {
// Success - set the text to indicate it,
// then set it back after 2 seconds.
var span = pre.querySelector("button span");
if (span) {
var oldText = span.innerText;
span.innerText = "COPIED!";
setTimeout(function() {
span.innerText = oldText;
}, 2000);
}
}, function() {
});
}
(function () {
// Add copy buttons to all the codeblocks.
var codeblocks = document.querySelectorAll('div > pre');
var button = document.createElement('button');
var span = document.createElement('span');
span.innerText = "COPY";
button.appendChild(span);
for (var i of codeblocks) {
var newButton = button.cloneNode(true);
newButton.addEventListener('click', copy_to_clipboard);
i.appendChild(newButton);
}
})();
</script>
</body>
</html>
|