user2284570 user2284570 - 3 months ago 6
Bash Question

Is there a way to change the SHELL program called by os.system()?

I’m in an embedded environment where

/bin/sh
doesn’t exist so when I call
posix.system()
or
os.system()
it always returns
-1
.

Because of that environment, the
subprocess
module isn’t available. I can’t create pipes too.

I’m using ɴᴀᴄʟ as glibc.

Setting the
SHELL
environment variable doesn’t seems to work (it still tries to open
/bin/sh
)
.

So is there a way to change the shell process invoked by
os.system()
 ?

Answer

No, you can't change what shell os.system() uses, because that function makes a call to the system() C function, and that function has the shell hardcoded to /bin/sh.

Use the subprocess.call() function instead, and set the executable argument to the shell you want to use:

subprocess.call("command", shell=True, shell='/bin/bash')

From the Popen() documentation (which underlies all subprocess functionality):

On Unix with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell.

and

If shell=True, on Unix the executable argument specifies a replacement shell for the default /bin/sh.

If you can't use subprocess and you can't use pipes, you'd be limited to the os.spawn*() functions; set mode to os.P_WAIT to wait for the exit code:

retval = os.spawnl(os.P_WAIT, '/bin/bash', '-c', 'command')
Comments