How to Open URLs in Google Chrome from the macOS Terminal

Tyler Tyler (281)
0

If you spend any amount of time working on the command line in macOS, you'll eventually want to automate the process of opening URLs in your favorite browser. In this guide, we'll learn how to open Chrome from the command line (or a shell script) on macOS.

In the following steps, we'll learn a few variations of the open command to learn how to:

  • Bring the Chrome window into focus
  • Open a specific URL in Chrome
  • Open a URL in a specific Chrome profile

Posted in these interests:

macos
PRIMARY
38 guides
chrome
13 guides
bash
5 guides

The open command accepts a -a option that allows us to specify which application we want to open.

So if you just want to open Google Chrome from the command line, it's as simple as this:

open -a "Google Chrome"

This will do nothing more than bring the Google Chrome window into focus.

To open a specific URL in Chrome, simply pass the URL as the first argument of the command, like this:

open -a "Google Chrome" https://howchoo.com

This will open your favorite website in your favorite browser.

Google About Version get profile directory

If you're like me and use multiple Chrome profiles, you'll want to be able to specify which profile to use when opening the URL. For this, we'll have to move away from the convenient open command.

Before we get to the command, we'll need to figure out the correct profile directory to use. Sadly, this will not be the same value as your profile name. To get your profile directory, open Chrome in the profile you want to use and navigate to chrome://version/. Find the item labeled Profile Path, and copy only the last part of the path. In my case, it's Profile 5.

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome https://howchoo.com --profile-directory=Profile\ 5

I know its not realistic to type this every time you want to open a URL. You could bake this into a script or see the next step where I'll cover using aliases and bash functions to make this less painful.

If you're going to open URLs frequently, there are a few ways to dramatically increase efficiency. The easiest way is to use bash aliases. An alias allows you to basically create a shortcut for longer commands.

Alias for opening URLs in Chrome

Here are a few examples (you can add them to your ~/.bash_profile).

alias gc="open -a Google\ Chrome"

Or if you'd like to create an alias to the command that let's you specify a profile, you could do the following:

alias gc="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --profile-directory=Profile\ 5"

Anytime you edit your bash_profile, you need to run source ~/.bash_profile or open a new shell to see the changes take effect.

With this, you can run:

gc  # Open or focus on the Chrome window
gc https://howchoo.com  # Open the url in Chrome

Similarly, the gc {url | command will open the url in Chrome, but it will use the specified profile every time.

Alias that accepts a URL from STDIN

In some cases, you might want to accept a URL from STDIN. The open command handles stdin the way we'd like, so if you're using the first alias, you don't need to do anything.

If you're using the alias for Google Chrome, one easy way is to create a second alias (using xargs) like this:

alias gcx="xargs /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --profile-directory=Profile\ 5"

This will allow you to do something like this:

echo "https://howchoo.com" | gcx

If you've got a better way of doing this, let me know in the comments below :)