Roy_Kid
Roy_Kid

Reputation: 51

subprocess.call() exec command in bash but I'm using zsh?

Im using Ubuntu 20 with zsh. When I using subprocess.call, it always using bash to exec command but not zsh. How should I do to fix this?

Upvotes: 1

Views: 2842

Answers (1)

tripleee
tripleee

Reputation: 189749

No, it uses sh regardless of which shell is your login shell.

There is a keyword argument to select a different shell, but you should generally run as little code as possible in a subshell; mixing nontrivial shell script with Python means the maintainer has to understand both languages.

whatever = subprocess.run(
    'echo $SHELL', 
    shell=True, executable='/usr/bin/zsh',
    check=True)

(This will echo your login shell, so the output would be /usr/bin/zsh even if you ran this without executable, or with Bash instead.)

In many situations, you should avoid shell=True entirely if you can.

Upvotes: 1

Related Questions