title: Why doesn't testing exit status work when output is redirected?
body: <p>I have been testing bash and found the following code:</p>
  <pre><code>#!/bin/bash
  
  [[ `which pacman` ]] &amp;&amp; echo pacman
  [[ `which apt-get` ]] &amp;&amp; echo apt-get
  </code></pre>
  <p>It will test what package manager is installed and echo it.</p>
  <p>On some systems, a failed <code>which</code> command prints the error to <code>stderr</code>.  I want to suppress all output from the <code>which</code> command.</p>
  <p>So I came up with the following code:</p>
  <pre><code>#!/bin/bash
  
  [[ `which pacman &gt;/dev/null 2&gt;&amp;1` ]] &amp;&amp; echo pacman
  [[ `which apt-get &gt;/dev/null 2&gt;&amp;1` ]] &amp;&amp; echo apt-get
  </code></pre>
  <p>But this doesn't output anything.  When I run each command on the command line like this:</p>
  <pre><code>which pacman &gt;/dev/null 2&gt;&amp;1 &amp;&amp; echo $?
  </code></pre>
  <p>On a system with <code>pacman</code>, it prints <code>0</code> which it should.  The <code>&amp;&amp;</code> also proves that the previous command succeeded.</p>
  <p>So why doesn't the redirection code work like it does on the command line?  What can I add to the script to make it work?</p>
  <p>This is really confusing to me, as I have never had this type of problem before.  Usually, any command that works on the command line should also work in a bash script, shouldn't it?</p>