首页 > 解决方案 > 尝试在 mac 上的 bash 脚本中运行 chrome 时出现问题

问题描述

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

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

标签: bashmacosshell

解决方案


/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome

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

另一种方法是使用引号,即foo "a b"实际上与foo a\ b. 当我们使用引号时,我们不会转义空格,所以应该是:

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

推荐阅读