Today I started thinking that how cool is do some Google searches straight from command line. First I try to find some ready tricks from the Internet, but I couldn’t find quickly anything good. So I decide do a simple Bash function that can take Google search as parameter and open Firefox Browser (tab) with Google search. Sounds easy… :)

First we need Google search URL without search parameter and it looks like this:

http://www.google.com/search?hl=en#q=

Next is good to do some tests with Firefox, URL and search words from command line:

firefox http://www.google.com/search?hl=en#q=test

Works as it should work

firefox http://www.google.com/search?hl=en#q=test more words

Not working

firefox http://www.google.com/search?hl=en#q=test+more+words

Works as it should work

So as you can see we need some URL encoding for this search words. Maybe simple Perl One Liner is good enough for this job (so you need Perl):

perl -pe's/([^-_.~A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg');

And then we could make our first Linux function on command line:

function encode() { echo -n $@ | perl -pe's/([^-_.~A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg'; }

Simply pipe parameters to our Perl one liner.
Note: encode function is separated because we want to use it later other searches too…

After encode function is added then we can test it with writing encode command and add some words after that:

encode test some words

Output:

test%20some%20words

Then we are ready to add the final Google search function:

function google() { firefox http://www.google.com/search?hl=en#q="`encode $@`" ;}

And then we are ready to test some real Google searches:

google some search words

Result:
New Firefox or Firefox tab is opened and google show search results…

The same type of search is really easy to implement also the other search engines by changing only the URL and check that whether encoding necessary. Of course it’s possible to use other browsers if you can open URLs straight from the command line.

Here is Yahoo search example:

function yahoo() { firefox http://search.yahoo.com/search?p="`encode $@`" ;}

Here is Bing search example:

function bing() { firefox http://www.bing.com/search?q="`encode $@`" ;}

Here is Amazon search example:

function amazon() { firefox http://www.amazon.com/s/ref=nb_ss?url=search-alias%3Daps&field-keywords="`encode $@`" ;}

Here is Wikipedia search example:

function wiki() { firefox http://en.wikipedia.org/w/index.php?title=Special%3ASearch&redirs=0&fulltext=Search&ns0=1&search="`encode $@`" ;}

Next step is add all these functions to the bashrc file (~/.bashrc):


function encode() { echo -n $@ | perl -pe's/([^-_.~A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg'; }
function google() { firefox http://www.google.com/search?hl=en#q="`encode $@`" ;}
function yahoo() { firefox http://search.yahoo.com/search?p="`encode $@`" ;}
function bing() { firefox http://www.bing.com/search?q="`encode $@`" ;}
function amazon() { firefox http://www.amazon.com/s/ref=nb_ss?field-keywords="`encode $@`" ;}
function wiki() { firefox http://en.wikipedia.org/w/index.php?search="`encode $@`" ;}

And now you can use these functions (google, yahoo, bing, amazon, wiki and encode) every time you use command line. And it was easy as pie… :)