我想将多个参数作为单个字符串参数传递给 bash 脚本给外部可执行文件(尤其是git)
我找到了几个建议这样的答案:
"'$*'"
"'$@'"
Run Code Online (Sandbox Code Playgroud)
这在传递到时看起来不错,echo但在传递到外部程序时失败,即使通过echo.
这是一个 MWE:
#!/bin/bash
# preparation
git init .
git add -A
echo git commit -m "'$@'" # works when copied to terminal
git commit -m "'$@'" # fails if more than one parameter given
git commit -m $(echo "'$@'") # fails if more than one parameter given
rm -rf .git # c
Run Code Online (Sandbox Code Playgroud)
导致:
$ bash test.sh test2 test2
empty Git-Repository in /tests/bash-scripts/.git/ initialized
git commit …Run Code Online (Sandbox Code Playgroud)