I have the following python function that allows me to run shell commands from within a python script:
import subprocess
def run_shell_command(cmd,cwd=None):
retVal = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, cwd=cwd);
retVal = retVal.stdout.read().strip('\n');
return(retVal);
This allows me to do things like:
output = run_shell_command("echo 'Hello world'")
My question is: with the definition of run_shell_command above, which type of shell is started? (e.g. login vs interactive).
Knowing which shell is started would help know which which dot files (e.g. .bashrc, .profile, etc.) are executed prior to my shell command.
What shell is run?
This is mentioned in the Python
subprocessdocumentation:/bin/shon Linux/MacOSX is typically an alias for bash (or bash-compatible – newer versions of Debian use dash), whereas on Unixes like Solaris, it might be classic Bourne Shell.For Windows, it’s usually
cmdorcommand.bat.Login shell or not via
popen?I just realized that I haven’t answered your 2nd question – but setting
shell=Truewill spawn a non-login shell (look @AndiDog’s source code link, the way the shell is getting forked would create a non-login shell).Security Implications
Also be aware that using
shell=True, while it allows you to use shell primitives and shortcuts, can also be a security risk, so be sure to check any possible inputs you might use for process spawning.