Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 545
I_dont_know Avatar asked Dec 19 '22 19:12

I_dont_know


2 Answers

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.
like image 148
I_dont_know Avatar answered Dec 28 '22 06:12

I_dont_know


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!

like image 40
Number16BusShelter Avatar answered Dec 28 '22 05:12

Number16BusShelter