Google query from terminal

Is there a way to launch a google query from the terminal? Like starting up google (as in firefox www.google.com) but with a search query? It would be nice to not have to install any extra programs and have single command to do this.


Solution 1:

browser google.com/search?q=query

Where browser is the desired web browser.

Solution 2:

You can add next function in your ~/.bashrc file:

function google { 
     Q="$@"; 
     GOOG_URL='https://www.google.com/search?q=';
     stream=$(exo-open "${GOOG_URL}${Q//\ /+}" | grep -oP '\/url\?q=.+?&amp' | sed 's|/url?q=||; s|&amp||'); 
     echo -e "${stream//\%/\x}"; 
}

Next, when you open a terminal you can run:

google query to search

or

google query to search &

to open in background your default browser at www.google.com including the search query.

Solution 3:

Here is a little script I use for exactly that with chrome:

#!/bin/bash -

FLAG="-i"
INCOG=""

if [ x"$1" == x"$FLAG" ]
then
        INCOG="--incognito"
        shift
fi


QUERY=$(echo "$*" | sed 's/+/%2b/g' | sed 's/#/%23/g' | tr -s ' ' '+')

nohup /opt/google/chrome/google-chrome $INCOG \-url www.google.com\/search\?sourceid\=chrome\&ie\=UTF\-8\&q\=$QUERY > /dev/null 2>&1 &
exit

I've included some html character replacement for # and + so I can search for c# and c++ when I need to - more could easily be added to suit your needs. I also have a -i option for the script to launch in incognito mode. I'm sure this could be adapted to firefox, but I don't use firefox.