<article>
<h2 id="preamble">Preamble</h2>
<p>This is a case where I came upon a thing in Bash I had never considered before and was pleased and surprised that there was a way of doing what I wanted to do! If this is completely obvious to you, apologies, but it wasn’t to me!</p>
<h2 id="overview">Overview</h2>
<p>Many programming languages have the concept of <a href="https://en.wikipedia.org/wiki/Short-circuit_evaluation">short-circuit evaluation</a> in Boolean expressions. What this means is that in an expression such as:</p>
<pre><code>A AND B</code></pre>
<p>if <code>A</code> is <strong>false</strong> then the whole expression must be <strong>false</strong>, and <code>B</code> doesn’t have to be evaluated. That is because both arguments to <code>AND</code> have to be <strong>true</strong> for the overall result to be <strong>true</strong>.</p>
<p>If <code>A</code> is <strong>true</strong> on the other hand, then <code>B</code> has to be evaluated to determine if the overall result is <strong>true</strong>.</p>
<p>Similarly with:</p>
<pre><code>A OR B</code></pre>
<p>if <code>A</code> is <strong>true</strong> then the whole expression must be <strong>true</strong> and <code>B</code> can be skipped without evaluation. This is because only one argument to <code>OR</code> needs to be <strong>true</strong> to return a <strong>true</strong> result.</p>
<p>If <code>A</code> is <strong>false</strong> on the other hand, then <code>B</code> has to be evaluated to determine if the overall result is <strong>false</strong>.</p>
<p>Both of these expressions are evaluated from left to right. This is not a given in all languages. Some use special operators such as <code>'and_then'</code> and <code>'or_else'</code> which explicitly perform short-circuiting and left-to-right evaluation.</p>
<h3 id="definition">Definition</h3>
<p>In simple terms, short-circuiting is where the evaluation of an expression is stopped as soon as its outcome is determined.</p>
<p>The Wikipedia article <a href="https://en.wikipedia.org/wiki/Short-circuit_evaluation">Short-circuit evaluation</a> defines it as:</p>
<blockquote>
<p>Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true.</p>
</blockquote>
<p>This article contains a table entitled <a href="https://en.wikipedia.org/wiki/Short-circuit_evaluation#Support_in_common_programming_and_scripting_languages"><em>Boolean operators in various languages</em></a> which shows details of how various programming and scripting languages cater for this feature.</p>
<h2 id="use-case">Use case</h2>
<p>I was writing a Bash script in which I wanted to ask questions about various steps - should they be done or not? Alternatively, I wanted to be able to set an option to run without interaction and assume the answer is <code>'yes'</code> to all questions.</p>
<p>I’d encountered <em>short-circuit evaluation</em> before in Pascal and Perl so I wondered if I could use it in Bash.</p>
<p>The expression I was trying to write was:</p>
<pre><code>if [[ $YES -eq 1 ]] || yes_no &#39;Create directory? %s &#39; &#39;N&#39;; then
    # Create directory
fi</code></pre>
<ul>
<li>Variable <code>YES</code> is being set through an option <code>'-Y'</code>; it’s normally set to zero but is set to 1 if the option is used.</li>
<li><code>yes_no</code> is a function I wrote, and talked about in <a href="https://hackerpublicradio.org/eps.php?id=2096">HPR episode 2096: “Useful Bash functions - part 2”</a>.</li>
</ul>
<p>The requirement was that if <code>YES</code> was set to 1 I didn’t want the function to be called at all.</p>
<p>I was a little surprised, and very happy, to find that this is what happens.</p>
<p>Here is the full example from the script that started me thinking about this issue - and therefore caused me to make this show:</p>
<pre><code>#
# We need a show directory. If it doesn&#39;t exist then we&#39;ll create it because
# other scripts will use it.
#
if [[ ! -d $SHOWDIR ]]; then
    echo &quot;${red}There is no directory for show $show${reset}&quot;

    #
    # If the -Y option was not chosen ask with &#39;yes_no&#39;. It -Y was chosen
    # we&#39;re to go ahead regardless. This relies on the fact that Bash
    # &quot;short-circuits&quot; logical expressions like this.
    #
    if [[ $YES -eq 1 ]] || yes_no &#39;Create directory? %s &#39; &#39;N&#39;; then
        mkdir &quot;$SHOWDIR&quot;
        _silent &quot;${green}Directory created for show $show${reset}&quot;
    else
        _silent &quot;${yellow}Not changed${reset}&quot;
    fi
fi</code></pre>
<p><strong>Notes</strong>:</p>
<ul>
<li>I have a Bash function that defines colours which is included into this script. That’s why you see <code>'echo "${red}...${reset}"'</code> in the above. I also have a function to turn off colour by setting the relevant variables to empty strings.</li>
<li>The <code>'yes_no'</code> function takes a prompt string with an (optional) <code>'%s'</code> placeholder for the expected inputs and default. This is followed by the default: <code>'N'</code>.</li>
<li>The function <code>'_silent'</code> writes the message given as its argument, depending on the setting of a <code>'SILENT'</code> variable set earlier.</li>
</ul>
<h2 id="should-it-be-used">Should it be used?</h2>
<h3 id="case-1">Case 1</h3>
<p>Bash uses short-circuiting in other contexts. This was discussed in the <em>Bash Tips</em> series, <a href="https://hackerpublicradio.org/eps.php?id=2649">episode 10</a> with the example:</p>
<pre><code>[ -e /some/file ] || exit 1</code></pre>
<p>Here the test is performed using <code>'-e'</code> to determine if <code>'/some/file'</code> exists. The result is either <strong>true</strong> or <strong>false</strong>. If the test returns <strong>true</strong> then the overall result of the <em>or</em> is <strong>true</strong> and the evaluation is short-circuited so that the <code>'exit 1'</code> is not invoked. If the test is <strong>false</strong> then the second expression has to be evaluated to determine the overall result, so the <code>'exit 1'</code> is invoked and the script exits.</p>
<p>Incidentally, the <code>'[ -e file ]'</code> construct is actually an instance of the <code>test</code> command so could be written:</p>
<pre><code>test -e /some/file || exit 1</code></pre>
<p>You might be familiar with command pipelines which use this technique, such as:</p>
<pre><code>sudo apt update &amp;&amp; sudo apt upgrade</code></pre>
<p>If the <code>'apt update'</code> is successful the <code>'apt upgrade'</code> is run. If it fails then the second command is not run.</p>
<h3 id="case-2">Case 2</h3>
<p>We have seen the example that prompted me to make this show:</p>
<pre><code>if [[ $YES -eq 1 ]] || yes_no &#39;Create directory? %s &#39; &#39;N&#39;; then
    # Create directory
fi</code></pre>
<p>This could have been written as:</p>
<pre><code>if [[ $YES -eq 1 ]]; then
    # Create directory
elif yes_no &#39;Create directory? %s &#39; &#39;N&#39;; then
    # Create directory
fi</code></pre>
<p>I prefer the first way, but it could be argued in a development environment that co-workers might find it confusing.</p>
<h3 id="conclusion">Conclusion</h3>
<p>So, my conclusion is that short-circuiting is a desirable feature that I will continue to use.</p>
<h2 id="links">Links</h2>
<ul>
<li>Wikipedia:
<ul>
<li><a href="https://en.wikipedia.org/wiki/Short-circuit_evaluation">Wikipedia article on short-circuit evaluation</a>
<ul>
<li><a href="https://en.wikipedia.org/wiki/Short-circuit_evaluation#Support_in_common_programming_and_scripting_languages">Table entitled <em>Boolean operators in various languages</em></a></li>
</ul></li>
<li><a href="https://en.wikipedia.org/wiki/Conditional_(programming)">Wikipedia article on conditional constructs in programming</a></li>
<li><a href="https://en.wikipedia.org/wiki/Truth-value">Wikipedia article on Truth values in logic, mathematics and programming</a></li>
</ul></li>
</ul>
<ul>
<li>HPR shows:
<ul>
<li><a href="https://hackerpublicradio.org/eps.php?id=2096"><em>Useful Bash functions - part 2</em></a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2649"><em>More ancillary Bash tips - 10</em></a></li>
</ul></li>
</ul>
</article>
