linuxbashautomationxdgutils

How to run xdg-open to execute a google search from command line?


If I can search google from the command line on Ubuntu, it would save me a lot of time in my workflow. If I run this it opens up Google.

xdg-open 'https://www.google.com/search?q="searchterm1" "searchterm2"'

I dont know how to make this google.sh accept inputs though.

Essentially I want to run this in the command line

./googler.sh searchterm1 searchterm2 searchterm3 

This will open this Google link in the browser

https://www.google.com/search?q= "searchterm1" "searchterm2" "searchterm3"

Solution

  • If you want to accept an arbitrary number of arguments, you can do something like this:

    #!/bin/sh
    
    quote_search_terms() {
        for term in "$@"; do
            echo "\"$term\" "
        done
    }
    
    xdg-open "https://www.google.com/search?q=$(quote_search_terms "$@")"
    

    This will give you behavior similar to what you've shown in your question.