Search code examples
bashgoogle-search

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?


Solution

  • 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.