Google search from OSX UNIX command line


Simple shell script to run a Google search from your UNIX shell command line. The code shown below is free to use and improve. I wrote it on the fly because I’m too lazy to start up a browser. ( I justify my laziness at the end of the post). As you can see it is crude and simple with the elegance of a pool of vomit. But it works and is pleasanter than vomiting. If you improve on it hope you care to share the code.

Code to Search Google from the command line

#! /bin/sh
#base path for home page url
url='http://www.google.com/search?client=safari&rls=en&q='
#get key words
usr=$1
#put it all together
open `echo $url$usr`

How Google search shell script works:

This script simply assembles your keywords into a URL that that pulls a Google search results page for your query. If you are not as me lazy you could type the URL this script generates into the address box of your browser.

One benefit I have notice is that you can run a search query outside your localised version of google – as in search using google.com instead of google.lk. Trivial I know but I wasn’t claiming this was a work of genius.🙂 This script uses the “open” UNIX program to launch the browser.

How to use this script

You just enter your search words as arguments after calling the script. If you got two or more “search words” just insert the + sign between each word. Being a simple script it doesn’t handle spaces. To add quote marks to specify exact key words – you will need to add them as %22 between each work.
EG: %22Google+command+line%22
instead of “Google+command+line”.

I did say this was a SIMPLE and CRUDE script. Those seeking more complexity should check out “Search Google from the command line” post on osxhints.com.

I use this script in conjunction with the “open” UNIX program which which is part of OS X’s UNIX applications. It might not be included on non OS X system. You could simply have the search results piped into a text file or some other application. Vast are the potential of shell scripting.

If you want to know why:

My home machine is a mac – but I run it like a UNIX machine. I use a UNIX shell terminal on start up to do most things including getting around to files and launching applications. Screen grab of my OS X desktop will give you the idea. I hardly use the OSX GUI. My horde of shell script and command line shortcuts cuts down on all that tiring mouse clicking.

I suppose the might be plenty of little app-lets that could make the task less command line orientated. But I’m too much of a slob to bother installing yet another program.

For those who must ask why OSX instead of Linux or one of the BSDs: I don’t know and I’m too lazy to find out.

2 comments

Say something

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s