<!DOCTYPE HTML> <html lang="en" class="sidebar-visible no-js light"> <head> <!-- Book generated using mdBook --> <meta charset="UTF-8"> <title>Conditional Expressions - Zsh Manual</title> <!-- Custom HTML head --> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="theme-color" content="#ffffff" /> <link rel="icon" href="favicon.svg"> <link rel="shortcut icon" href="favicon.png"> <link rel="stylesheet" href="css/variables.css"> <link rel="stylesheet" href="css/general.css"> <link rel="stylesheet" href="css/chrome.css"> <link rel="stylesheet" href="css/print.css" media="print"> <!-- Fonts --> <link rel="stylesheet" href="FontAwesome/css/font-awesome.css"> <link rel="stylesheet" href="fonts/fonts.css"> <!-- Highlight.js Stylesheets --> <link rel="stylesheet" href="highlight.css"> <link rel="stylesheet" href="tomorrow-night.css"> <link rel="stylesheet" href="ayu-highlight.css"> <!-- Custom theme stylesheets --> </head> <body> <!-- Provide site root to javascript --> <script type="text/javascript"> var path_to_root = ""; var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light"; </script> <!-- Work around some values being stored in localStorage wrapped in quotes --> <script type="text/javascript"> try { var theme = localStorage.getItem('mdbook-theme'); var sidebar = localStorage.getItem('mdbook-sidebar'); if (theme.startsWith('"') && theme.endsWith('"')) { localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1)); } if (sidebar.startsWith('"') && sidebar.endsWith('"')) { localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1)); } } catch (e) { } </script> <!-- Set the theme before any content is loaded, prevents flash --> <script type="text/javascript"> var theme; try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } if (theme === null || theme === undefined) { theme = default_theme; } var html = document.querySelector('html'); html.classList.remove('no-js') html.classList.remove('light') html.classList.add(theme); html.classList.add('js'); </script> <!-- Hide / unhide sidebar before it is displayed --> <script type="text/javascript"> var html = document.querySelector('html'); var sidebar = 'hidden'; if (document.body.clientWidth >= 1080) { try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { } sidebar = sidebar || 'visible'; } html.classList.remove('sidebar-visible'); html.classList.add("sidebar-" + sidebar); </script> <nav id="sidebar" class="sidebar" aria-label="Table of contents"> <div class="sidebar-scrollbox"> <ol class="chapter"><li class="chapter-item expanded "><a href="The-Z-Shell-Manual.html"><strong aria-hidden="true">1.</strong> The Z Shell Manual</a></li><li class="chapter-item expanded "><a href="Introduction.html"><strong aria-hidden="true">2.</strong> Introduction</a></li><li class="chapter-item expanded "><a href="Roadmap.html"><strong aria-hidden="true">3.</strong> Roadmap</a></li><li class="chapter-item expanded "><a href="Invocation.html"><strong aria-hidden="true">4.</strong> Invocation</a></li><li class="chapter-item expanded "><a href="Files.html"><strong aria-hidden="true">5.</strong> Files</a></li><li class="chapter-item expanded "><a href="Shell-Grammar.html"><strong aria-hidden="true">6.</strong> Shell Grammar</a></li><li class="chapter-item expanded "><a href="Redirection.html"><strong aria-hidden="true">7.</strong> Redirection</a></li><li class="chapter-item expanded "><a href="Command-Execution.html"><strong aria-hidden="true">8.</strong> Command Execution</a></li><li class="chapter-item expanded "><a href="Functions.html"><strong aria-hidden="true">9.</strong> Functions</a></li><li class="chapter-item expanded "><a href="Jobs-_0026-Signals.html"><strong aria-hidden="true">10.</strong> Jobs & Signals</a></li><li class="chapter-item expanded "><a href="Arithmetic-Evaluation.html"><strong aria-hidden="true">11.</strong> Arithmetic Evaluation</a></li><li class="chapter-item expanded "><a href="Conditional-Expressions.html" class="active"><strong aria-hidden="true">12.</strong> Conditional Expressions</a></li><li class="chapter-item expanded "><a href="Prompt-Expansion.html"><strong aria-hidden="true">13.</strong> Prompt Expansion</a></li><li class="chapter-item expanded "><a href="Expansion.html"><strong aria-hidden="true">14.</strong> Expansion</a></li><li class="chapter-item expanded "><a href="Parameters.html"><strong aria-hidden="true">15.</strong> Parameters</a></li><li class="chapter-item expanded "><a href="Options.html"><strong aria-hidden="true">16.</strong> Options</a></li><li class="chapter-item expanded "><a href="Shell-Builtin-Commands.html"><strong aria-hidden="true">17.</strong> Shell Builtin Commands</a></li><li class="chapter-item expanded "><a href="Zsh-Line-Editor.html"><strong aria-hidden="true">18.</strong> Zsh Line Editor</a></li><li class="chapter-item expanded "><a href="Completion-Widgets.html"><strong aria-hidden="true">19.</strong> Completion Widgets</a></li><li class="chapter-item expanded "><a href="Completion-System.html"><strong aria-hidden="true">20.</strong> Completion System</a></li><li class="chapter-item expanded "><a href="Completion-Using-compctl.html"><strong aria-hidden="true">21.</strong> Completion Using compctl</a></li><li class="chapter-item expanded "><a href="Zsh-Modules.html"><strong aria-hidden="true">22.</strong> Zsh Modules</a></li><li class="chapter-item expanded "><a href="Calendar-Function-System.html"><strong aria-hidden="true">23.</strong> Calendar Function System</a></li><li class="chapter-item expanded "><a href="TCP-Function-System.html"><strong aria-hidden="true">24.</strong> TCP Function System</a></li><li class="chapter-item expanded "><a href="Zftp-Function-System.html"><strong aria-hidden="true">25.</strong> Zftp Function System</a></li><li class="chapter-item expanded "><a href="User-Contributions.html"><strong aria-hidden="true">26.</strong> User Contributions</a></li></ol> </div> <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div> </nav> <div id="page-wrapper" class="page-wrapper"> <div class="page"> <div id="menu-bar-hover-placeholder"></div> <div id="menu-bar" class="menu-bar sticky bordered"> <div class="left-buttons"> <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar"> <i class="fa fa-bars"></i> </button> <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list"> <i class="fa fa-paint-brush"></i> </button> <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu"> <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li> <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li> <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li> <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li> <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li> </ul> <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar"> <i class="fa fa-search"></i> </button> </div> <h1 class="menu-title">Zsh Manual</h1> <div class="right-buttons"> <a href="print.html" title="Print this book" aria-label="Print this book"> <i id="print-button" class="fa fa-print"></i> </a> </div> </div> <div id="search-wrapper" class="hidden"> <form id="searchbar-outer" class="searchbar-outer"> <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header"> </form> <div id="searchresults-outer" class="searchresults-outer hidden"> <div id="searchresults-header" class="searchresults-header"></div> <ul id="searchresults"> </ul> </div> </div> <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM --> <script type="text/javascript"> document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible'); document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible'); Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) { link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1); }); </script> <div id="content" class="content"> <main> <!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> <p><strong>Table of Contents</strong> <em>generated with <a href="https://github.com/thlorenz/doctoc">DocToc</a></em></p> <ul> <li><a href="#12-conditional-expressions">12 Conditional Expressions</a></li> </ul> <!-- END doctoc generated TOC please keep comment here to allow auto update --> <p><span id="Conditional-Expressions"></span> <span id="Conditional-Expressions-1"></span></p> <h1 id="12-conditional-expressions"><a class="header" href="#12-conditional-expressions">12 Conditional Expressions</a></h1> <p><span id="index-conditional-expressions"></span> <span id="index-expressions_002c-conditional"></span></p> <p>A <em>conditional expression</em> is used with the <code>[[</code> compound command to test attributes of files and to compare strings. Each expression can be constructed from one or more of the following unary or binary expressions:</p> <ul> <li> <p><code>-a</code> <code>file</code><br /> true if <code>file</code> exists.</p> </li> <li> <p><code>-b</code> <code>file</code><br /> true if <code>file</code> exists and is a block special file.</p> </li> <li> <p><code>-c</code> <code>file</code><br /> true if <code>file</code> exists and is a character special file.</p> </li> <li> <p><code>-d</code> <code>file</code><br /> true if <code>file</code> exists and is a directory.</p> </li> <li> <p><code>-e</code> <code>file</code><br /> true if <code>file</code> exists.</p> </li> <li> <p><code>-f</code> <code>file</code><br /> true if <code>file</code> exists and is a regular file.</p> </li> <li> <p><code>-g</code> <code>file</code><br /> true if <code>file</code> exists and has its setgid bit set.</p> </li> <li> <p><code>-h</code> <code>file</code><br /> true if <code>file</code> exists and is a symbolic link.</p> </li> <li> <p><code>-k</code> <code>file</code><br /> true if <code>file</code> exists and has its sticky bit set.</p> </li> <li> <p><code>-n</code> <code>string</code><br /> true if length of <code>string</code> is non-zero.</p> </li> <li> <p><code>-o</code> <code>option</code><br /> true if option named <code>option</code> is on. <code>option</code> may be a single character, in which case it is a single letter option name. (See <a href="Options.html#Specifying-Options">Specifying Options</a>.)</p> <p>When no option named <code>option</code> exists, and the <code>POSIX_BUILTINS</code> option hasn’t been set, return 3 with a warning. If that option is set, return 1 with no warning.</p> </li> <li> <p><code>-p</code> <code>file</code><br /> true if <code>file</code> exists and is a FIFO special file (named pipe).</p> </li> <li> <p><code>-r</code> <code>file</code><br /> true if <code>file</code> exists and is readable by current process.</p> </li> <li> <p><code>-s</code> <code>file</code><br /> true if <code>file</code> exists and has size greater than zero.</p> </li> <li> <p><code>-t</code> <code>fd</code><br /> true if file descriptor number <code>fd</code> is open and associated with a terminal device. (note: <code>fd</code> is not optional)</p> </li> <li> <p><code>-u</code> <code>file</code><br /> true if <code>file</code> exists and has its setuid bit set.</p> </li> <li> <p><code>-v</code> <code>varname</code><br /> true if shell variable <code>varname</code> is set.</p> </li> <li> <p><code>-w</code> <code>file</code><br /> <code>-x</code> <code>file</code><br /> <code>-z</code> <code>string</code><br /> true if length of <code>string</code> is zero.</p> </li> <li> <p><code>-L</code> <code>file</code><br /> true if <code>file</code> exists and is a symbolic link.</p> </li> <li> <p><code>-O</code> <code>file</code><br /> true if <code>file</code> exists and is owned by the effective user ID of this process.</p> </li> <li> <p><code>-G</code> <code>file</code><br /> true if <code>file</code> exists and its group matches the effective group ID of this process.</p> </li> <li> <p><code>-S</code> <code>file</code><br /> true if <code>file</code> exists and is a socket.</p> </li> <li> <p><code>-N</code> <code>file</code><br /> true if <code>file</code> exists and its access time is not newer than its modification time.</p> </li> <li> <p><code>file1</code> <code>-nt</code> <code>file2</code><br /> true if <code>file1</code> exists and is newer than <code>file2</code>.</p> </li> <li> <p><code>file1</code> <code>-ot</code> <code>file2</code><br /> true if <code>file1</code> exists and is older than <code>file2</code>.</p> </li> <li> <p><code>file1</code> <code>-ef</code> <code>file2</code><br /> true if <code>file1</code> and <code>file2</code> exist and refer to the same file.</p> </li> <li> <p><code>string</code> <code>=</code> <code>pattern</code><br /> <code>string</code> <code>==</code> <code>pattern</code><br /> true if <code>string</code> matches <code>pattern</code>. The two forms are exactly equivalent. The ‘<code>=</code>’ form is the traditional shell syntax (and hence the only one generally used with the <code>test</code> and <code>[</code> builtins); the ‘<code>==</code>’ form provides compatibility with other sorts of computer language.</p> </li> <li> <p><code>string</code> <code>!=</code> <code>pattern</code><br /> true if <code>string</code> does not match <code>pattern</code>.</p> </li> <li> <p><code>string</code> <code>=~</code> <code>regexp</code><br /> true if <code>string</code> matches the regular expression <code>regexp</code>. If the option <code>RE_MATCH_PCRE</code> is set <code>regexp</code> is tested as a PCRE regular expression using the <code>zsh/pcre</code> module, else it is tested as a POSIX extended regular expression using the <code>zsh/regex</code> module. Upon successful match, some variables will be updated; no variables are changed if the matching fails.</p> <p>If the option <code>BASH_REMATCH</code> is not set the scalar parameter <code>MATCH</code> is set to the substring that matched the pattern and the integer parameters <code>MBEGIN</code> and <code>MEND</code> to the index of the start and end, respectively, of the match in <code>string</code>, such that if <code>string</code> is contained in variable <code>var</code> the expression ‘<code>${var[$MBEGIN,$MEND]}</code>’ is identical to ‘<code>$MATCH</code>’. The setting of the option <code>KSH_ARRAYS</code> is respected. Likewise, the array <code>match</code> is set to the substrings that matched parenthesised subexpressions and the arrays <code>mbegin</code> and <code>mend</code> to the indices of the start and end positions, respectively, of the substrings within <code>string</code>. The arrays are not set if there were no parenthesised subexpressions. For example, if the string ‘<code>a short string</code>’ is matched against the regular expression ‘<code>s(...)t</code>’, then (assuming the option <code>KSH_ARRAYS</code> is not set) <code>MATCH</code>, <code>MBEGIN</code> and <code>MEND</code> are ‘<code>short</code>’, <code>3</code> and <code>7</code>, respectively, while <code>match</code>, <code>mbegin</code> and <code>mend</code> are single entry arrays containing the strings ‘<code>hor</code>’, ‘<code>4</code>’ and ‘<code>6</code>’, respectively.</p> <p>If the option <code>BASH_REMATCH</code> is set the array <code>BASH_REMATCH</code> is set to the substring that matched the pattern followed by the substrings that matched parenthesised subexpressions within the pattern.</p> </li> <li> <p><code>string1</code> <code><</code> <code>string2</code><br /> true if <code>string1</code> comes before <code>string2</code> based on ASCII value of their characters.</p> </li> <li> <p><code>string1</code> <code>></code> <code>string2</code><br /> true if <code>string1</code> comes after <code>string2</code> based on ASCII value of their characters.</p> </li> <li> <p><code>exp1</code> <code>-eq</code> <code>exp2</code><br /> true if <code>exp1</code> is numerically equal to <code>exp2</code>. Note that for purely numeric comparisons use of the <code>((``...``))</code> builtin described in <a href="Arithmetic-Evaluation.html#Arithmetic-Evaluation">Arithmetic Evaluation</a> is more convenient than conditional expressions.</p> </li> <li> <p><code>exp1</code> <code>-ne</code> <code>exp2</code><br /> true if <code>exp1</code> is numerically not equal to <code>exp2</code>.</p> </li> <li> <p><code>exp1</code> <code>-lt</code> <code>exp2</code><br /> true if <code>exp1</code> is numerically less than <code>exp2</code>.</p> </li> <li> <p><code>exp1</code> <code>-gt</code> <code>exp2</code><br /> true if <code>exp1</code> is numerically greater than <code>exp2</code>.</p> </li> <li> <p><code>exp1</code> <code>-le</code> <code>exp2</code><br /> true if <code>exp1</code> is numerically less than or equal to <code>exp2</code>.</p> </li> <li> <p><code>exp1</code> <code>-ge</code> <code>exp2</code><br /> true if <code>exp1</code> is numerically greater than or equal to <code>exp2</code>.</p> </li> <li> <p><code>(</code> <code>exp</code> <code>)</code><br /> true if <code>exp</code> is true.</p> </li> <li> <p><code>!</code> <code>exp</code><br /> true if <code>exp</code> is false.</p> </li> <li> <p><code>exp1</code> <code>&&</code> <code>exp2</code><br /> true if <code>exp1</code> and <code>exp2</code> are both true.</p> </li> <li> <p><code>exp1</code> <code>||</code> <code>exp2</code><br /> true if either <code>exp1</code> or <code>exp2</code> is true.</p> </li> </ul> <p>For compatibility, if there is a single argument that is not syntactically significant, typically a variable, the condition is treated as a test for whether the expression expands as a string of non-zero length. In other words, <code>[[ $var ]]</code> is the same as <code>[[ -n $var ]]</code>. It is recommended that the second, explicit, form be used where possible.</p> <p>Normal shell expansion is performed on the <code>file</code>, <code>string</code> and <code>pattern</code> arguments, but the result of each expansion is constrained to be a single word, similar to the effect of double quotes.</p> <p>Filename generation is not performed on any form of argument to conditions. However, it can be forced in any case where normal shell expansion is valid and when the option <code>EXTENDED_GLOB</code> is in effect by using an explicit glob qualifier of the form <code>(#q)</code> at the end of the string. A normal glob qualifier expression may appear between the ‘<code>q</code>’ and the closing parenthesis; if none appears the expression has no effect beyond causing filename generation. The results of filename generation are joined together to form a single word, as with the results of other forms of expansion.</p> <p>This special use of filename generation is only available with the <code>[[</code> syntax. If the condition occurs within the <code>[</code> or <code>test</code> builtin commands then globbing occurs instead as part of normal command line expansion before the condition is evaluated. In this case it may generate multiple words which are likely to confuse the syntax of the test command.</p> <p>For example,</p> <div class="example"> <pre><code class="language-example">[[ -n file*(#qN) ]] </code></pre> </div> <p>produces status zero if and only if there is at least one file in the current directory beginning with the string ‘<code>file</code>’. The globbing qualifier <code>N</code> ensures that the expression is empty if there is no matching file.</p> <p>Pattern metacharacters are active for the <code>pattern</code> arguments; the patterns are the same as those used for filename generation, see <a href="Expansion.html#Filename-Generation">Filename Generation</a>, but there is no special behaviour of ‘<code>/</code>’ nor initial dots, and no glob qualifiers are allowed.</p> <p>In each of the above expressions, if <code>file</code> is of the form ‘<code>/dev/fd/``n</code>’, where <code>n</code> is an integer, then the test applied to the open file whose descriptor number is <code>n</code>, even if the underlying system does not support the <code>/dev/fd</code> directory.</p> <p>In the forms which do numeric comparison, the expressions <code>exp</code> undergo arithmetic expansion as if they were enclosed in <code>$((``...``))</code>.</p> <p>For example, the following:</p> <div class="example"> <pre><code class="language-example">[[ ( -f foo || -f bar ) && $report = y* ]] && print File exists. </code></pre> </div> <p>tests if either file <code>foo</code> or file <code>bar</code> exists, and if so, if the value of the parameter <code>report</code> begins with ‘<code>y</code>’; if the complete condition is true, the message ‘<code>File exists.</code>’ is printed.</p> <hr /> <p>This document was generated on <em>February 15, 2020</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 5.0</em></a>.<br /> Zsh version 5.8, released on February 14, 2020.</p> </main> <nav class="nav-wrapper" aria-label="Page navigation"> <!-- Mobile navigation buttons --> <a rel="prev" href="Arithmetic-Evaluation.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> <i class="fa fa-angle-left"></i> </a> <a rel="next" href="Prompt-Expansion.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> <i class="fa fa-angle-right"></i> </a> <div style="clear: both"></div> </nav> </div> </div> <nav class="nav-wide-wrapper" aria-label="Page navigation"> <a rel="prev" href="Arithmetic-Evaluation.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left"> <i class="fa fa-angle-left"></i> </a> <a rel="next" href="Prompt-Expansion.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right"> <i class="fa fa-angle-right"></i> </a> </nav> </div> <!-- Livereload script (if served using the cli tool) --> <script type="text/javascript"> var socket = new WebSocket("ws://localhost:3000/__livereload"); socket.onmessage = function (event) { if (event.data === "reload") { socket.close(); location.reload(); } }; window.onbeforeunload = function() { socket.close(); } </script> <script type="text/javascript"> window.playground_copyable = true; </script> <script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script> <script src="mark.min.js" type="text/javascript" charset="utf-8"></script> <script src="searcher.js" type="text/javascript" charset="utf-8"></script> <script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script> <script src="highlight.js" type="text/javascript" charset="utf-8"></script> <script src="book.js" type="text/javascript" charset="utf-8"></script> <!-- Custom JS scripts --> </body> </html>