5

This question already has an answer here:

In default installation of macOS, Python and Bash are installed in /usr/bin and /bin directories respectively.

$ which python
/usr/bin/python

$ which bash
/bin/bash

So it's appropriate files can be opened using the command-line as:

python foo.py

Along similar lines, I want the ability to open an HTML file with Google Chrome using the command-line as:

$ chrome foo.html

But I get the following output:

bash: chrome: command not found

An alternate way to achieve the desired workflow is make Google Chrome as the default browser and execute the following command:

open foo.html

How do I achieve the former workflow?

marked as duplicate by Allan, fsb, Ɱark Ƭ, Glorfindel Mar 31 '18 at 19:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • for your own scripts you can add a folder such as ~/bin to your PATH variable, or use /usr/local/bin which normally is user-writable on macs. And btw, open path/to/file.html should work without problem, provided that you allowed chrome to take over the extensions. – ccpizza Feb 17 at 14:15
5

There is no need to create or copy any file into /usr/bin directory. A simple solution would be create an alias as shown below:

alias chrome="open -a \"Google Chrome\".

Now you open the HTML file as you described by executing:

chrome foo.html

You may wish to export the alias to make the behaviour permanent placing the alias in .bash_profile in your home directory.

  • ty, how to export alias to make it permanent? – Algebra Mar 31 '18 at 15:23
  • Updated the answer with instructions on how to export the alias. – Nimesh Neema Mar 31 '18 at 15:28
  • I add the alias to ~/.bash_profile $ chrome foo.html -bash: chrome: command not found.Still, I should manual reset alias when restarting the terminal. – Algebra Apr 1 '18 at 7:16
  • the command above is incomplete, it should be: alias chrome="open -a \"Google Chrome\"." then remember to run: source .bash_profile – Runnerdave Jul 12 at 1:16
6

/usr/bin is a system directory owned by root user and it isn't possible to create any files in it due to Apple's System Integrity Protection introduced in Mac OS X El Capitan.

The command you are looking for is:

open -a "Google Chrome" foo.html

Description of command:

open -a "ApplicationToUse" file-to-open.extension

More details on the open command via man open on the command line.

In general, the above command-line can be used to launch any GUI app to open associated file.

open command is built into macOS. For more open command tips and tricks, refer this article, Shell tricks: the OS X open command.

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