7

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.

11
browser google.com/search?q=query

Where browser is the desired web browser.

  • You are welcome. I'd suggest asking at superuser.com next time since this is not very Ubuntu-related. – Alex Jul 17 '13 at 13:47
3

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.