0

I tried this:

 a="\"Google Chrome\"" 

and I tried

 a="'Google Chrome'" 

but no go. How can I accomplish? For this script here:

birthBrowser(){ local a if [ $# -eq 0 ] then a="Google Chrome" fi if [ $# -eq 1 ] then a="$1" fi if [ $# -gt 1 ] then a="$1" echo "Too many arguments" fi open -a $a } 

It seems to keep reading only the Chrome part and not treating "Google Chrome" as one argument.

For example open -a "Google Chrome" works in the console.

4
  • This question has been answered here: stackoverflow.com/questions/35586627/… Commented Mar 23, 2016 at 1:04
  • 1
    Escaping quotes makes them data, not syntax. Unless you literally want quotation marks to be treated as data by the open command (which, I promise you, you don't), you don't want to escape the quotes. Instead, you should escape the expansion. Commented Mar 23, 2016 at 1:07
  • 1
    @theman, it does what you ask for; that's just not what you want. It escapes the quotes, which is what you're asking how to do, but if you want open to work correctly, then those quotes shouldn't be escaped. Commented Mar 23, 2016 at 1:08
  • 1
    BTW, if you want automated checking for this kind of error, see shellcheck.net Commented Mar 23, 2016 at 1:14

1 Answer 1

1

If your intended behavior is identical to this:

open -a "Google Chrome" 

...then your scripted use should look like this:

a="Google Chrome" open -a "$a" 

In the usage above, all quotes are syntactical, whereas if you literally escape quotes as part of a string, then they become data, and lose their special meaning as syntax.

See BashFAQ #50 for an in-depth explanation of why trying to escape syntactical quotes is the Wrong Thing.

Sign up to request clarification or add additional context in comments.

4 Comments

It's so counterintuitive because it's backwards-compatible with something that was designed in the 1970s. Welcome to UNIX.
Bourne. Not completely backwards-compatible with Bourne, mind you -- the early-90s POSIX sh standard did away with accepting ^ as a pipe character -- but with a limited set of exceptions.
Indeed. Bash is a superset of POSIX sh (mostly; breaks a few minor parts, like the echo spec, by default), which Bourne predates by decades. Which is to say that the name was chosen to be cute, not to be accurate.
@theman, ...and the "kindergartener" if/fi / case/esac, etc. thing didn't originate with Bourne either -- that was borrowed from a still earlier (1950s and 60s-era) language, ALGOL. If inclined to spearhead a debate, I'd argue that the "kindergarteners" are the people who don't know their field's history. :)