user6085320
user6085320

Reputation:

How to escape quotes in bash?

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.

Upvotes: 0

Views: 89

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295914

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.

Upvotes: 1

Related Questions