9

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?

1
  • 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
    Commented Feb 17, 2019 at 14:15

2 Answers 2

10

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.

5
  • ty, how to export alias to make it permanent?
    – Wizard
    Commented Mar 31, 2018 at 15:23
  • Updated the answer with instructions on how to export the alias.
    – Nimesh Neema
    Commented Mar 31, 2018 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.
    – Wizard
    Commented Apr 1, 2018 at 7:16
  • 1
    the command above is incomplete, it should be: alias chrome="open -a \"Google Chrome\"." then remember to run: source .bash_profile
    – Runnerdave
    Commented Jul 12, 2019 at 1:16
  • Command above need the end ": alias chrome="open -a \"Google Chrome\""
    – Vincent
    Commented Jun 28, 2020 at 7:30
8

/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 .