What's the difference between subprocess.Popen(“echo $HOME”… and subprocess.Popen([“echo”, “$HOME”]

后端 未结 3 641
不知归路
不知归路 2021-01-15 08:38

I cannot get it it\'s bash related or python subprocess, but results are different:

>>> subprocess.Popen(\"echo $HOME\", shell=True, stdout=subproce         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-15 09:25

    In the documentation found on https://docs.python.org/2/library/subprocess.html#popen-constructor, if you look at the shell argument you will find

    The shell argument (which defaults to False) specifies whether to use the shell as the program to execute. If shell is True, it is recommended to pass args as a string rather than as a sequence.

    Which means that when you execute the second command it runs as echo and hence you get just a new line.

提交回复
热议问题