Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python的argv和bash?_Python_Bash_Argv - Fatal编程技术网

Python的argv和bash?

Python的argv和bash?,python,bash,argv,Python,Bash,Argv,我一直在学习pythons sys.argv并消除一些误解。当我在bash中通过命令行传递不同的字符时,我注意到: script.py import sys def test(x): return x print test(sys.argv) >>>python script.py [first, second, third] 将打印: ['script.py', '[first,', 'second,', 'third]'] 及 但是: 这是python还是

我一直在学习pythons sys.argv并消除一些误解。当我在bash中通过命令行传递不同的字符时,我注意到:

script.py

import sys
def test(x):
    return x

print test(sys.argv)

>>>python script.py [first, second, third]
将打印:

['script.py', '[first,', 'second,', 'third]']

但是:


这是python还是bash,可能两者都有?有什么原因吗?

是bash;Paren在子shell中运行命令链

pwd ; ( cd /tmp ; pwd ) ; pwd
如果你想在辩论中使用paren,你需要引用它们

echo '(foo)'

这是狂欢;Paren在子shell中运行命令链

pwd ; ( cd /tmp ; pwd ) ; pwd
如果你想在辩论中使用paren,你需要引用它们

echo '(foo)'
这是bash shell,如错误消息所示:

bash: syntax error near unexpected token `('
bash出于自己的目的使用括号对命令进行分组

尝试像这样转义括号:

   python script.py "(first,second,third)"
这也可能起作用:

  python script.py \(first,second,third\)
这是bash shell,如错误消息所示:

bash: syntax error near unexpected token `('
bash出于自己的目的使用括号对命令进行分组

尝试像这样转义括号:

   python script.py "(first,second,third)"
这也可能起作用:

  python script.py \(first,second,third\)

正如@Ignacio所说,尝试引用bash命令行中的每个参数

但是,您似乎将bash视为Python的方式。它们是不同的东西

下面是一个很好的基本shell脚本教程:


您只需要阅读第2章就可以知道问题的答案。

正如@Ignacio所说,尝试引用bash命令行中的每个参数

但是,您似乎将bash视为Python的方式。它们是不同的东西

下面是一个很好的基本shell脚本教程:


您只需阅读第2章即可知道问题的答案。

谢谢您的链接!我已经完成了一些shell脚本编写,但是可以学到更多:您能指出第2章中提到这一点的页面吗?我通读了一遍,没有看到任何提到它的内容。有两个部分是相关的,啊,我知道转义引号,我想括号的用法会有所不同。再次感谢您的回复!谢谢你的链接!我已经完成了一些shell脚本编写,但是可以学到更多:您能指出第2章中提到这一点的页面吗?我通读了一遍,没有看到任何提到它的内容。有两个部分是相关的,啊,我知道转义引号,我想括号的用法会有所不同。再次感谢您的回复!