I_dont_know
I_dont_know

Reputation: 371

Google search from Terminal

I am trying to write a script so as to Google-search in terminal. Below is the code :

google.sh

#!/bin/bash
echo "Searching for : $@"
for term in $@ ; do
    echo "$term"
    $search = $search%20$term
done
    open "http://www.google.com/search?q=$search"

Whenever I try to run the script as :

./google.sh some string

I get the error as :

Searching for : some string
some
./google.sh: line 5: =: command not found
string
./google.sh: line 5: =: command not found

Also the google home page opens up in the browser.Please tell me what am I doing wrong here?

Upvotes: 6

Views: 10797

Answers (3)

Alberto Salvia Novella
Alberto Salvia Novella

Reputation: 1250

For searching in Google, I have this:

googleSearch [query]

....................

Upvotes: 1

Number16BusShelter
Number16BusShelter

Reputation: 630

I've made this custom aliases. Works for me. OSX

Hope this helps someone!

I added this commands to my .zshrc

alias chrome='{read -r arr; open -a "Google Chrome" "${arr}"} <<<'

alias browser='{read -r arr; chrome ${arr} } <<<'

alias google='{read -r arr; browser "https://google.com/search?q=${arr}";} <<<'

Example: google 'How do I google from terminal?'

It will open browser of your choice. Chrome for me, but you can also use Safari or Firefox. By passing arguments right into search query you are able to go directly to search results!

Upvotes: 1

I_dont_know
I_dont_know

Reputation: 371

I got what was the problem. These are the modifications that I made and my code worked.

#!/bin/bash
echo "Searching for : $@"
for term in $@ ; do
    echo "$term"
    search="$search%20$term"
done
    open "http://www.google.com/search?q=$search"
  1. Removed $ from search at line 5
  2. Put $search%20$term within quotes as "$search%20$term"
  3. And as suggested removed spaces from line 5.

Upvotes: 9

Related Questions