命令行解析器在sh中调用python时无法处理空格

wai*_*lou 0 python shell command-line

a.py:
for arg in sys.argv:
    print arg

b.sh:
python a.py $*
Run Code Online (Sandbox Code Playgroud)

情况1:

python a.py "123 456"
Run Code Online (Sandbox Code Playgroud)

得到:

123 456

情况2:

/bin/sh b.sh "123 456"
Run Code Online (Sandbox Code Playgroud)

得到:

123

456

似乎"123 456"将削减两个参数,我如何修改b.sh,使a.py可以将"123 456"视为一个arg.

Wil*_*ell 5

你需要报价:

python a.py "$*"
Run Code Online (Sandbox Code Playgroud)

此外,如果您使用"$@"而不是"$*"它将扩展为多个参数.