Start a Web Search in a GUI Browser from the Command Line

Aug 22, 2013 - 1 Comment

With the help of a simple command line function, you can quickly initiate a web search in your GUI web browser of choice right from the Terminal app. We’ll cover a few examples, demonstrating searching the web with Google, Bing, Yahoo, and Wikipedia, and using a variety of different web browsers, including Chrome, Safari, and Firefox. Because the command syntax is fairly straightforward, you can further customize the search functions or apps used as you see fit.

Search the web from the command line

Setting the Web Search Function in Bash Profile

The function sample provided here will use Google search in the Chrome browser, and we’re assuming that you are using bash as your shell, which is the default in OS X. Check out the other command syntax options below to make adjustments to the web search or browser accordingly:

  • Head to the Terminal (found in /Applications/Utilities/, but you already knew that) and open .bash_profile, for this walkthrough we’ll use nano to accomplish this task since it’s simple:
  • nano .bash_profile

  • Copy and paste the following syntax at the end of your bash_profile on a new line:
  • function google() { open /Applications/Google\ Chrome.app/ "http://www.google.com/search?q= $1"; }

  • Now hit Control+O to save the changes, then Control+X to quit out of nano and return to the command line

Now you can initiate a Google search through Chrome directly from the command line, all you need to do is type “google [phrase]” and a new google search will launch in the Chrome browser. For example, to search google for “apple” you would just type the following:

google apple

For phrases with multiple search terms, use quotes like so:

google "MacBook Air is the industry leader for thin and light notebooks"

Each new search of Google will summon a new Chrome browser window.

Prefer to use Safari or Firefox? Or maybe you want to search Bing or Wikipedia? All you need to do is change or add the appropriate open command string along with the proper search URL to point at the alternate browser app and search engine.

Alternate Web Search Command Line Functions

The command syntax is fairly straightforward and is in line with the following syntax:

function NAME() { open /path/to/application.app/ "SEARCH_URL"; }

Make your own, or use one of the additional sample command functions for various search engines and web browsers. Remember to place each command string on a new line in .bash_profile to avoid conflicts.

Start a Google search in Safari from the command line

function google() { open /Applications/Safari.app/ "http://www.google.com/search?q= $1"; }

Start a Google search in Firefox from the command line

function google() { open /Applications/Firefox.app/ "http://www.google.com/search?q= $1"; }

Similarly to adjusting which browser to use, you can switch the search string to Yahoo, Bing, Wikipedia, or whatever other web search you want to use by placing the following strings into .bash_profile:

Search Bing in chrome from the command line

function bing() { open /Applications/Google\ Chrome.app/ "http://www.bing.com/search?q= $1"; }

Search Yahoo in chrome from the command line

function yahoo() { open /Applications/Google\ Chrome.app/ "http://www.yahoo.com/search?q= $1"; }

Search Wikipedia in Chrome from the Command Line

function wikipedia() { open /Applications/Google\ Chrome.app/ "http://en.wikipedia.org/wiki/Special:Search?search= $1";}

Using any of these alternative search functions is done the same way as the initial google example, you only need to change the initial command string to initiate the search. And yes, this also works to send searches to the X11 version of Lynx (Links) if you feel like being slightly meta.

Moving away from queries, one could also use a variation of this trick to open your favorite website directly from the command line, for example:

function osxdaily() { open /Applications/Google\ Chrome.app/ "http://osxdaily.com"; }

That said, if you aren’t using queries, it would probably make more sense to use a general alias instead.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: Paul Horowitz in Command Line, Tips & Tricks

One Comment

» Comments RSS Feed

  1. bingeboy says:

    Thanks for the functions. The google one made my night!

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates