<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<?xml-stylesheet href="https://baszerr.eu/lib/exe/css.php?s=feed" type="text/css"?>
<rss version="2.0">
    <channel xmlns:g="http://base.google.com/ns/1.0">
        <title>BaSzErr - blog:2023:08:22</title>
        <description></description>
        <link>https://baszerr.eu/</link>
        <lastBuildDate>Wed, 29 Apr 2026 02:41:35 +0000</lastBuildDate>
        <generator>FeedCreator 1.8</generator>
        <image>
            <url>https://baszerr.eu/lib/exe/fetch.php?media=wiki:dokuwiki.svg</url>
            <title>BaSzErr</title>
            <link>https://baszerr.eu/</link>
        </image>
        <item>
            <title>2023-08-22_-_interesting_bash_feature</title>
            <link>https://baszerr.eu/doku.php?id=blog:2023:08:22:2023-08-22_-_interesting_bash_feature</link>
            <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;interesting_bash_feature&quot;&gt;2023-08-22 - interesting bash feature&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
talk is cheap, so i&amp;#039;ll show you some code instead:
&lt;/p&gt;
&lt;pre class=&quot;code bash&quot;&gt;&lt;span class=&quot;co0&quot;&gt;#!/bin/bash&lt;/span&gt;
&lt;span class=&quot;kw1&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;re5&quot;&gt;-e&lt;/span&gt;
&lt;span class=&quot;kw2&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;kw3&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;this never shows up&amp;quot;&lt;/span&gt;
&lt;span class=&quot;kw3&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;result: $?&amp;quot;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
what will it show? &lt;code&gt;set -e&lt;/code&gt; is there so nothing, right? well – no. it will print this:
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt; result: 1&lt;/pre&gt;

&lt;p&gt;
…and returns exit code &lt;code&gt;0&lt;/code&gt;. now the open question is: how on earth?! return from the &lt;code&gt;false &amp;amp;&amp;amp; whatever&lt;/code&gt; is clearly non-zero, yet the next line gets executed! stating that previous line ended with an error.
&lt;/p&gt;

&lt;p&gt;
let&amp;#039;s now compare it with almost identical piece of code:
&lt;/p&gt;
&lt;pre class=&quot;code bash&quot;&gt;&lt;span class=&quot;co0&quot;&gt;#!/bin/bash&lt;/span&gt;
&lt;span class=&quot;kw1&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;re5&quot;&gt;-e&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span class=&quot;kw2&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;sy0&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;kw3&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;this never shows up&amp;quot;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;kw3&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;result: $?&amp;quot;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
the output? nothing + return code of &lt;code&gt;1&lt;/code&gt;. just as expected!
&lt;/p&gt;

&lt;p&gt;
spoiler alert – it&amp;#039;s not a bug. it&amp;#039;s a feature. just very non-obvious one. &lt;a href=&quot;https://stackoverflow.com/questions/25794905/why-does-set-e-true-false-true-not-exit&quot; class=&quot;urlextern&quot; title=&quot;https://stackoverflow.com/questions/25794905/why-does-set-e-true-false-true-not-exit&quot; rel=&quot;ugc nofollow&quot;&gt;here&amp;#039;s a nice explanation&lt;/a&gt;. &lt;abbr title=&quot;Too long; didn&amp;#039;t read&quot;&gt;TL;DR&lt;/abbr&gt; version is that &lt;code&gt;set -e&lt;/code&gt; exits with an error on non-handled error. eg. this would not fail the script:
&lt;/p&gt;
&lt;pre class=&quot;code bash&quot;&gt;&lt;span class=&quot;kw1&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#91;&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;re5&quot;&gt;-eq&lt;/span&gt; &lt;span class=&quot;nu0&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#93;&lt;/span&gt;
&lt;span class=&quot;kw1&quot;&gt;then&lt;/span&gt;
  &lt;span class=&quot;kw3&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;st0&quot;&gt;&amp;quot;wtf?!&amp;quot;&lt;/span&gt;
&lt;span class=&quot;kw1&quot;&gt;fi&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
despite &lt;code&gt;[ 1 -eq 2 ]&lt;/code&gt; is clearly false. now if we look at &lt;code&gt;foo &amp;amp;&amp;amp; bar&lt;/code&gt; through the same lenses, here &lt;code&gt;foo&lt;/code&gt;&amp;#039;s failure is considered “handled”, as there&amp;#039;s a post-action &lt;code&gt;&amp;amp;&amp;amp; bar&lt;/code&gt;, thus &lt;code&gt;$?&lt;/code&gt; is set as expected, but script does not stop.
&lt;/p&gt;

&lt;p&gt;
i found this one while inspecting other&amp;#039;s person script. it wasn&amp;#039;t really clear what&amp;#039;s the problem. in the future – when you write a shell script, do use &lt;code&gt;set -e&lt;/code&gt; but try keeping 1 statement per line. it makes everyone&amp;#039;s lifes easier. :)
&lt;/p&gt;

&lt;p&gt;
thx &lt;a href=&quot;https://pragmatic-architect.com/&quot; class=&quot;urlextern&quot; title=&quot;https://pragmatic-architect.com/&quot; rel=&quot;ugc nofollow&quot;&gt;Wojtek&lt;/a&gt; for helping out with this one! :)
&lt;/p&gt;

&lt;/div&gt;
</description>
            <author>anonymous@undisclosed.example.com (Anonymous)</author>
            <pubDate>Tue, 22 Aug 2023 19:36:36 +0000</pubDate>
        </item>
    </channel>
</rss>
