Subprocess return code different than "echo $?"
<p>I'm using subprocess to call a bash command in Python, and I'm getting a different return code than what the shell shows me.</p>

<pre><code>import subprocess
def check_code(cmd):
    print "received command '%s'" % (cmd)
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    p.wait()
    print "p.returncode is '%d'" % (p.returncode)
    exit()
    if p.returncode == 0:
        return True
    else:
        return False
    #End if there was a return code at all
#End get_code()
</code></pre>

<p>When sent "ls /dev/dsk &amp;> /dev/null", check_code returns 0, but "echo $?" produces "2" in the terminal:</p>

<pre><code>Welcome to Dana version 0.7
Now there is Dana AND ZOL

received command 'ls /dev/dsk &amp;&gt; /dev/null'
p.returncode is '0'
root@Ubuntu-14:~# ls /dev/dsk &amp;&gt; /dev/null
root@Ubuntu-14:~# echo $?
2
root@Ubuntu-14:~#
</code></pre>

<p>Does anyone know what's going on here?</p>