title: Race conditions in shell command lists
body: <p>How safe are command lists in shell (or bash) from race conditions?</p>
  
  <pre><code>if [ -h "$dir" ]; then
    echo 'Directory exists and is a symlink'
    exit 1
  fi
  cd "$dir"
  </code></pre>
  
  <p>The above code is obviously prone to race conditions: An attacker can create a symlink after the check, but still before changing the directory into it.</p>
  
  <p>Does the same apply to <code>||</code> command lists? In other words: is the below command immune to race conditions, or do the same rules as above still apply?</p>
  
  <pre><code>[ -h "$dir" ] || cd "$dir"
  </code></pre>
  
  <p>With error message:</p>
  
  <pre><code>[ -h "$dir" ]
    &amp;&amp; { echo 'Directory exists and is a symlink'; exit 1; }
    || cd "$dir"
  </code></pre>