Looking through the source code of the subprocess module, because using a list of arguments with shell=True will do the equivalent ...
/bin/sh -c 'echo' '$ATESTVARIABLE'
... when do you want...
/bin/sh -c 'echo $ATESTVARIABLE'
The following works for me ...
import os, subprocess os.environ['ATESTVARIABLE'] = 'value' value = subprocess.check_output('echo $ATESTVARIABLE', shell=True) assert 'value' in value
Update
FWIW, the difference between the two is that the first form ...
/bin/sh -c 'echo' '$ATESTVARIABLE'
... will just call the shell of the built-in echo with no parameters and set $0 to the literal string '$ATESTVARIABLE' , for example ...
$ /bin/sh -c 'echo $0' /bin/sh $ /bin/sh -c 'echo $0' '$ATESTVARIABLE' $ATESTVARIABLE
... whereas the second form ...
/bin/sh -c 'echo $ATESTVARIABLE'
... will invoke the built-in echo shell with a single parameter equal to the value of the ATESTVARIABLE environment ATESTVARIABLE .
source share