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

Aug 22, 2013 - 3 Comments

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/ "https://osxdaily.com"; }

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

.

Related articles:

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

3 Comments

» Comments RSS Feed

  1. jimmy says:

    I’m attempting to do this and every time I write the code to the .bash_profile and try to use the function google it says that google is not found.

    What am I doing wrong?

  2. Eric says:

    Great function! Thanks…

    One can use the “-a” handle to skip the full path, just the name of the program. Which is great way to open a program when you don’t know exactly where it is in the file system (like Wi-Fi Diagnostics). :)

    open -a “Wi-Fi Diagnostics”

    # DuckDuckGo/Safari
    function ddg() { open -a Safari “https://duckduckgo.com/?q=$1”; }

    ddg osxdaily

    ddg ‘!gi yosmite’

    (single quotes with “!”, or “\!bang foo bar” )

  3. bingeboy says:

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

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

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

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site