9

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?

4 Answers 4

11

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!

Sign up to request clarification or add additional context in comments.

2 Comments

I am that someone. This is what I was looking for. Thank you for noting this :)
Yes, this is so true when one is piping stdout from a single command to multiple commands using tee, something like command out=stdout | tee >(command_1 in=stdin) >(command_2 in=stdin). Thanks for posting this, thumbs up~
9

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.

1 Comment

Indeed, /bin/sh (which is almost always some form of bourne shell) is almost always what is meant when anything *nix-related says "the shell" without qualification. It may also be useful to note that if you really need to execute some snippet under a specific non-bourne shell, you could pass the function something like '/path/to/tcsh -c "your tcsh snippet here"'.
5

os.system() just calls the system() system call ("man 3 system"). On most *nixes this means you get /bin/sh.

Note that export VAR=val is technically not standard syntax (though bash understands it, and I think ksh does too). It will not work on systems where /bin/sh is actually the Bourne shell. On those systems you need to export and set as separate commands. (This will work with bash too.)

1 Comment

Confirm this behavior. So the os.system() is no longer useful. I am running some scripts that require bash, and this os.system is giving me hard time. So in the future I will always use the subprocess : subprocess.call(cmd, shell=True, executable="/bin/bash")
2

If your command is a shell file, and the file is executable, and the file begins with "#!", you can pick your shell.

#!/bin/zsh
Do Some Stuff

You can write this file and then execute it with subprocess.Popen(filename,shell=True) and you'll be able to use any shell you want.

Also, be sure to read this about os.system and subprocess.Popen.

2 Comments

i was going to point out that shell=True is not necessary, but then it occurred to me: is it the the shell that's responsible for interpreting shebangs and acting accordingly?
Correct. The shell interprets the "magic" bytes "#!" to see what other shell should really be using this file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.