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.

  • This question has been answered here: http://stackoverflow.com/questions/35586627/how-to-escape-double-quotes-in-bash – Dustin Mar 23 '16 at 01: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. – Charles Duffy Mar 23 '16 at 01: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. – Charles Duffy Mar 23 '16 at 01:08
  • 1
    BTW, if you want automated checking for this kind of error, see http://shellcheck.net/ – Charles Duffy Mar 23 '16 at 01:14

1 Answers1

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • It's so counterintuitive because it's backwards-compatible with something that was designed in the 1970s. Welcome to UNIX. – Charles Duffy Mar 23 '16 at 01:12
  • 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. – Charles Duffy Mar 23 '16 at 01:15
  • 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*. – Charles Duffy Mar 23 '16 at 01:17
  • @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. :) – Charles Duffy Mar 23 '16 at 02:46