Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Long ago, when all we had was <code>sh</code>, booleans where handled by relying on a convention of the <code>test</code> program where <code>test</code> returns a false exit status if run with no arguments. This allows one to think of a variable that is unset as false and variable set to any value as true. Today, test is builtin to <code>bash</code> and is commonly known by its one character alias <code>[</code> (or an executable to use in shells lacking it, as dolmen notes):</p> <pre><code>FLAG="up or &lt;set&gt;" if [ "$FLAG" ] ; then echo 'Is true' else echo 'Is false' fi # unset FLAG # also works FLAG= if [ "$FLAG" ] ; then echo 'Continues true' else echo 'Turned false' fi </code></pre> <p>Because of quoting conventions, script writers prefer to use the compound command <code>[[</code> that mimics <code>test</code> but has nicer syntax: variables with spaces do not need to be quoted, one can use <code>&amp;&amp;</code> and <code>||</code> as logical operators with weird precedence, and there are no POSIX limitations on the number of terms.</p> <p>For example, to determine if FLAG is set and COUNT is a number greater than 1:</p> <pre><code>FLAG="u p" COUNT=3 if [[ $FLAG &amp;&amp; $COUNT -gt '1' ]] ; then echo 'Flag up, count bigger than 1' else echo 'Nope' fi </code></pre> <p><a href="https://stackoverflow.com/a/3870055/2267285">This stuff can get confusing</a> when spaces, zero length strings, and null variables are all needed and also when your script needs to work with several shells.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload