Subprocess calls the wrong version of 'time'
<p>I'm trying to call the shell keyword version of 'time' via </p>

<pre><code>subprocess.check_output('time ls',stderr=subprocess.STDOUT,shell=True)
</code></pre>

<p>this is giving me the output (which is /usr/bin/time output):</p>

<pre><code>0.00user 0.00system 0:00.00elapsed ?%CPU (0avgtext+0avgdata 1992maxresident)k
0inputs+0outputs (0major+85minor)pagefaults 0swaps
</code></pre>

<p>rather than the shell keyword output:</p>

<pre><code>real    0m0.001s
user    0m0.000s
sys     0m0.000s
</code></pre>

<p>For some reason subprocess.check_output is using the /usr/bin/time version of time.</p>

<p>Could someone tell me what is happening here? Thank you.</p>

<p>It is because <code>time</code> is a also a built in function in Bash. So you need to specify that you want to use Bash as your shell:</p>

<pre><code>print subprocess.check_output('time ls', stderr=subprocess.STDOUT,\
    shell=True, executable='/bin/bash')
</code></pre>

python linux shell time subprocess