Actual meaning of 'shell=True' in subprocess

后端 未结 5 1610
梦毁少年i
梦毁少年i 2020-11-21 05:05

I am calling different processes with the subprocess module. However, I have a question.

In the following codes:

callProcess = subproces         


        
5条回答
  •  耶瑟儿~
    2020-11-21 05:37

    >>> import subprocess
    >>> subprocess.call('echo $HOME')
    Traceback (most recent call last):
    ...
    OSError: [Errno 2] No such file or directory
    >>>
    >>> subprocess.call('echo $HOME', shell=True)
    /user/khong
    0
    

    Setting the shell argument to a true value causes subprocess to spawn an intermediate shell process, and tell it to run the command. In other words, using an intermediate shell means that variables, glob patterns, and other special shell features in the command string are processed before the command is run. Here, in the example, $HOME was processed before the echo command. Actually, this is the case of command with shell expansion while the command ls -l considered as a simple command.

    source: Subprocess Module

提交回复
热议问题