Evaluating bash "&&" exit codes behaviour : <p>We had a recent experience with bash that even we found a solution, it keeps twisting my mind. How does bash evaluates the <code>&amp;&amp;</code> expression in terms of return codes?</p>

<p>Executing this script, that should fail because <code>myrandomcommand</code> does not exist:</p>

<pre><code>#!/bin/bash

set -e

echo "foo"
myrandomcommand
echo "bar"
</code></pre>

<p>The result is the expected one:</p>

<pre><code>~ &gt; bash foo.sh 
foo
foo.sh: line 6: myrandomcommand: command not found
[exited with 127]
~ &gt; echo $?
127
</code></pre>

<p>But changing slightly the code using the <code>&amp;&amp;</code> expression:</p>

<pre><code>#!/bin/bash

set -e

echo "foo"
myrandomcommand &amp;&amp; ls
echo "bar"
</code></pre>

<p>The <code>ls</code> statement is not executed (since the first statement fails and does not evaluate the second statement), but the script behaves very different:</p>

<pre><code>~ &gt; bash foo.sh 
foo
foo.sh: line 6: myrandomcommand: command not found
bar                  # ('bar' is printed now)
~ &gt; echo $?
0
</code></pre>

<p>We found out that using the expression between parenthesis <code>(myrandomcommand &amp;&amp; ls)</code> it works as expected (like the first example), but I would like to know why.</p>
