23

I want to create command in terminal that would allow me to use

chrome index.html

and have the the given file open in Google Chrome.

How can I get this working?

  • I'm not near a Mac, so this is untested. You'll need the full path to Chrome's executable i.e. something like /Applications/Chrome/MacOS/Chrome followed by the HTML file's full path. All of this assumes the Chrome executable accepts command line argument. – Global nomad Feb 27 '13 at 3:31
49

You can use the open command with the -a flag to open a file or location in Chrome:

open -a "Google Chrome" index.html

This also works with URLs, i.e. open -a "Google Chrome" http://www.apple.com.

  • 4
    You can also make it just like in the question using alias: alias chrome='open -a "Google Chrome"' chrome index.html – valbaca Feb 27 '13 at 6:08
  • 4
    Yes, but that needs to be set in your bash profile or similar, otherwise it will go away once you close your Terminal session. – robmathers Feb 28 '13 at 2:26
  • good answer. do you know how to do the same but instead opening in a new Chrome window? – chharvey May 13 '16 at 2:33
  • On MacOS (OS X) you could write this in terminal: open -a "/Applications/Google Chrome.app" index.html – Netsi1964 Oct 29 '16 at 8:00
10

I found this way more beautiful:

  1. Edit ~/.bash_profile file and add the following line alias chrome="open -a 'Google Chrome'"
  2. Save and close the file.
  3. Either run source ~/.bash_profile or open a new window in Terminal.

You can now open the file, file.html, by running: chrome file.html on the command line.

1

When using this from a script or some automation tool I prefer to alias to the complete binary so I have access to all the command line options, (like --version ...)

alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
chrome --version
chrome -open index.html

Then if you want to have this alias permanently you can add it to your .bash_profile manually or using this little snippet:

echo "alias chrome=\"/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"\" >> ~/.bash_profile

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .