Getting os.system to work in Python Console? I do this in the Python Console in Blender 2.64 on Ubuntu 11.04:

&gt;&gt;&gt; import os
&gt;&gt;&gt; os.system('ls')
0

&gt;&gt;&gt; os.system('ls /')
0

&gt;&gt;&gt; os.system('echo aa')
0


If that 0 is return status, it implies the command should have executed successfully - yet nothing is printed to the terminal? However, if I do:

&gt;&gt;&gt; os.system('echo aa &gt; /tmp/test.txt')
0


... then there is a /tmp/test.txt file created, with the expected contents ?!

So, the problem is apparently getting the stdout of the system shell routed to the Python console. Is there any way to get this done, so I can see the output of, say, ls or echo in the Python console?
Ok, it seems subprocess can be used instead; note here you have to specify the shell explicitly, e.g. try pasting this script in Python Console:

import subprocess
p = subprocess.Popen(['bash', '-c', 'echo AAaa'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print(out, err)


The output to that is:

&gt;&gt;&gt; ...
&gt;&gt;&gt; print(out, err)
b'AAaa\n' b''


Note that if you intend to do "piping" bash commands, things get a tad bit more complicated (see Python - How to call bash commands with pipe? - Stack Overflow); so for instance, to call echo BBbb | xsel -b and actually have it succeed (that is, have the string end up on clipboard), you should split the two processes, and establish the pipe via Python:

import subprocess
import shlex
p1 = subprocess.Popen(shlex.split("echo BBbb"), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p2 = subprocess.Popen(shlex.split("xsel -b"), stdin=p1.stdout)
out, err = p2.communicate()
print(out, err)


The output to this looks like:

&gt;&gt;&gt; ...
&gt;&gt;&gt; print(out, err)
None None


... but the string BBbb does end up on clipboard, as intended.