There is a script I use that sets up virtual hosts for me and then opens up the url using safari, however, I'm trying to change the script so it opens up in Google Chrome instead.

This line is giving me problems.

OPEN_COMMAND="/usr/bin/open -a /Applications/Google Chrome.app"

I have also tried...

OPEN_COMMAND="/usr/bin/open -a /Applications/Google\ Chrome.app"


OPEN_COMMAND="/usr/bin/open -a /Applications/'Google Chrome.app'"

The error says the file doesn't exist.

This is where the command is used at the bottom of the script...

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Launch the new URL in the browser
/bin/echo -n "Launching virtualhost... "
/bin/echo "done"

The script is hosted here: http://code.google.com/p/virtualhost-sh/

  • That line just sets a variable. You need to show us how the variable is used. – Matthew Flaschen Jul 31 '10 at 0:02
  • I edited my question with that information + the link to the source code. – Seth Jul 31 '10 at 0:28

This is actually a bit tricky. When you put your command into quotes you already escape the special characters in that string.

If the string gets expanded when you invoke the variable as command the (automatically) escaped charackters get unescaped. This leads to this somewhat unexpected behaviour because all the whitespaces in that string are interpreted the same way but should be interpreted differently.

This can be achived by putting command and filepath into seperate variables and only qoute the part where you have to escape the whitespaces:


COMMAND="open -a"
APPPATH="/Applications/Google Chrome.app"


For your edit:

OPEN_COMMAND="/usr/bin/open -a"
APPPATH="/Applications/Google Chrome.app"
| improve this answer | |
  • I tried that, but it leads to errors. Other commands in the script are not found now. The PATH="... line is the one causing those, because when it is removed the script works fine for creating/removing virtual hosts again -minus the opening of the browser part at the end of course. – Seth Jul 31 '10 at 1:58
  • PATH contains the system path, which explains the errors when you overwrite it; choose a different variable name. In general, you should avoid caps except for variables you're exporting into the environment. – Matthew Flaschen Jul 31 '10 at 3:00
  • My bad. Thanks for clarifying Matthew. Edited to correct that. – matthias krull Jul 31 '10 at 9:04

See BashFAQ #50 -- there's no form of quoting/escaping you can do when setting the command variable that'll work, because after it's used it gets broken into words without any regard to what quotes, escapes, etc it may happen to contain. There are, however, a few ways to change both the definition and invocation to make it work (roughly in order from most to least flexible):

Use a shell function rather than a variable:

open_command() { open -a "/Applications/Google Chrome.app" "$@"; }
open_command "http://$VIRTUALHOST/"

Use an array rather than a simple variable:

open_command=( open -a "/Applications/Google Chrome.app" )
"${open_command[@]}" "http://$VIRTUALHOST/"

Use separate variables (like what mugen kenichi suggested, but with a variable name that won't clobber PATH):

open_command="open -a"
open_app="/Applications/Google Chrome.app"
$open_command "$open_app" "http://$VIRTUALHOST/"

(note that this last one won't let you fall back to using the default browser -- there's no good way to leave off the application.)

| improve this answer | |
  • This should be the accepted answer. – matthias krull Mar 27 '15 at 15:34

The third one ought to work. If it's not, then that means that something is expanding the string multiple times, which means you need to escape the spaces multiple times. The usual cause of this is that somewhere elsewhere in the script is referring to $OPEN_COMMAND without putting quotes round it. Ensure that you're doing stuff like:


...and not:


The other trick is to change the place in the script where it's trying to run $OPEN_COMMAND to:


...to ensure the quotes have made it through intact.

| improve this answer | |
  • I'm noticing there are not quotes around any of the commands. It works fine if I replace Google Chrome.app with Firefox.app. The script can be found here: code.google.com/p/virtualhost-sh – Seth Jul 31 '10 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.