Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.system() execute command under which linux shell?

I am using /bin/tcsh as my default shell.

However, the tcsh style command os.system('setenv VAR val') doesn't work for me. But os.system('export VAR=val') works.

So my question is how can I know the os.system() run command under which shell?

like image 802
limi Avatar asked May 25 '09 03:05

limi


2 Answers

Was just reading Executing BASH from Python, then 17.1. subprocess — Subprocess management — Python v2.7.3 documentation, and I saw the executable argument; and it seems to work:

$ python
Python 2.7.1+ (r271:86832, Sep 27 2012, 21:16:52) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.popen("echo $0").read()
sh
>>> import subprocess
>>> print subprocess.call("echo $0", shell=True).read()
/bin/sh
>>> print subprocess.Popen("echo $0", stdout=subprocess.PIPE, shell=True).stdout.read()
/bin/sh
>>> print subprocess.Popen("echo $0", stdout=subprocess.PIPE, shell=True, executable="/bin/bash").stdout.read()
/bin/bash
>>> print subprocess.Popen("cat <(echo TEST)", stdout=subprocess.PIPE, shell=True).stdout.read()
/bin/sh: Syntax error: "(" unexpected
>>> print subprocess.Popen("cat <(echo TEST)", stdout=subprocess.PIPE, shell=True, executable="/bin/bash").stdout.read()
TEST

Hope this helps someone,
Cheers!

like image 76
sdaau Avatar answered Oct 25 '22 03:10

sdaau


These days you should be using the Subprocess module instead of os.system(). According to the documentation there, the default shell is /bin/sh. I believe that os.system() works the same way.

Edit: I should also mention that the subprocess module allows you to set the environment available to the executing process through the env parameter.

like image 41
Kamil Kisiel Avatar answered Oct 25 '22 03:10

Kamil Kisiel