尝试在mac上的bash脚本中运行chrome时出现问题



我正试图通过mac上的bash脚本获得chrome的版本。我可以直接从终端运行以下命令fine:

macbook-pro:jenkins_server$ /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --version
Google Chrome 87.0.4280.67 

但当我试图把这样的东西放在bash脚本的一边时:

EXEC="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
"$EXEC" --version

我得到以下结果:

macbook-pro:jenkins_server$ ./test_script.sh 
./test_script.sh: line 2: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome: No such file or directory
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome

在这里,您转义了空格((,告诉shell这个字符串是完整的,而不是程序及其参数。

另一种选择是使用引号,即foo "a b"实际上与foo a b相同。当我们使用引号时,我们不会转义空格,所以应该是:

EXEC="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"

您使用了双引号。"foo bar"已经等价于foo bar(实际上是foo bar,但除了空格之外,没有一个字符具有需要转义的特殊含义,因此例如f计算为f(。

那么,"foo bar"等于foo\ bar;带引号的反斜杠是文字,而不是引用下面的空格。

最新更新