Search Google from the command line

Oct 09, '02 09:36:01AM

Contributed by: mithras

Often I'm working at the command line, and want to do a quick Google search on a topic. So I wrote a tiny shell script to make this easy, saving several clicks!

To use the script, simply type google your search terms while you're in Terminal. For example, typing google mac os x turns up pages about our favorite OS (this site is #3!). If you're on your local machine, the search results open in your favorite browser. If you're logged in via SSH, the search results open in the links command-line browser, which you can download here.

You can download the script from me, or copy and paste the following:

#!/bin/sh
query=""
for this_query_term in $@
do
query="${query}${this_query_term}+"
done
url="http://www.google.com/search?q=${query}"

remote_addr=`who am i | awk -F\( '{print $2}' | sed 's/)//'`

if [ -z "$remote_addr" ]; then
open "$url"
else
links "$url"
fi
Just copy-and-paste the script into a text file (call it 'google' to match the examples listed here), and make it executable (chmod 755 google). You'll want to place it somewhere that is in your path, such as /usr/bin, or better, put it in ~/bin and add that to your path.

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20021009063601684