opike
opike

Reputation: 7595

Issue trying to run chrome in a bash script on mac

I'm attempting to get the version of chrome through a bash script on mac. I can run the following command fine directly from the terminal:

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

But when I try to put something like this in side of a bash script:

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

I get the following result:

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

Upvotes: 0

Views: 1203

Answers (2)

chepner
chepner

Reputation: 532333

You double-quoted. "foo bar" is already equivalent to foo\ bar (really, \f\o\o\ \b\a\r, but except for the space, none of the characters has a special meaning that needed to be escaped, so e.g. \f evaluates to f).

"foo\ bar", then, is equivalent to foo\\\ bar; the quoted backslash is literal, rather than quoting the following space.

Upvotes: 0

Jorenar
Jorenar

Reputation: 2884

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

Here you escaped spaces (\ ) to tell the shell that this string is whole, not program and its arguments.

Alternative is to use quotes, i.e. foo "a b" is effectively the same as foo a\ b. When we are using quotes, we don't escape spaces, so it should be:

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

Upvotes: 3

Related Questions