Skip to main content
 首页 » 编程设计

Python 的 argv 和 bash

2025年05月04日22unruledboy

我一直在学习和消除对 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 script.py {first, second, third} 
 
['script.py', '{first,','second,','third}'] 

但是:

>>>python script.py (first,second,third) 
 
bash: syntax error near unexpected token `(' 

这是 python 还是 bash,也许两者都是?有什么理由吗?

请您参考如下方法:

这是庆典; parens 在子 shell 中运行命令链。

pwd ; ( cd /tmp ; pwd ) ; pwd 

如果你想在论证中使用它们,你需要引用它们。

echo '(foo)'