How to search the internet from Linux terminal?


Welcome to the next pikoTutorial !

Linux terminal is a basic tool for many software engineers and the place where we often spend a lot of our time during the work day. Even if you’re not doing anything in the terminal right now, it is for sure opened for the entire time on your second screen or at the bottom of your IDE.

This is the reason why it’s a good idea to incorporate as many of the every day actions as possible directly into the terminal. We’ve already covered how to embed hex/dec conversions, so now it’s time to see how search the internet faster, without manually going to the internet browser.

How to open any website from the terminal?

If you use Google Chrome, the syntax for opening any website is very simple:

Bash
google-chrome "www.somepage.com"

How to search in Google from the terminal?

The easiest way to search Google from the terminal is to add a bash function to you .bashrc file:

Bash
google() {
    local query="$*"
    if [ -z "$query" ]; then
        google-chrome "https://www.google.com"
    else
        query=$(echo "$query" | jq -sRr @uri)
        google-chrome "https://www.google.com/search?q=$query"
    fi
}

It defines a function google which:

  • if called without any additional arguments, it just opens “www.google.com”
  • if called with any argument, it builds a Google query and opens web browser displaying Google search results for that query

For example, if I call:

Bash
google c++ future

I get my browser immediately opened with all the Google results for “c++ future” search term. Line query=$(echo "$query" | jq -sRr @uri) is crucial because it allows you to use non-alphanumerical characters (like “+”) in your queries:

  • jq is a lightweight JSON processor which we’re using here to URL-encode the input string
  • -s flag treats the input as a single string instead of individual lines
  • -R flag tells jq to treat the input as raw text instead JSON data
  • -r flag makes jq output raw text instead of JSON-encoded
  • @uri encodes the string as a URL, so special characters are replaced with percent ASCII-encoded equivalents

What else is worth having in the .bashrc file?

Search StackOverflow from the terminal

Bash
so() {
    local query="$*"
    if [ -z "$query" ]; then
        google-chrome "https://stackoverflow.com"
    else
        query=$(echo "$query" | jq -sRr @uri)
        google-chrome "https://stackoverflow.com/search?q=$query"
    fi
}

If you get some unfamiliar error prompt, it’s often useful to just call:

Bash
so c++ use of deleted function

Search YouTube from the terminal

Bash
yt() {
    local query="$*"
    if [ -z "$query" ]; then
        google-chrome "https://www.youtube.com"
    else
        query=$(echo "$query" | jq -sRr @uri)
        google-chrome "https://www.youtube.com/results?search_query=$query"
    fi
}

YouTube is priceless when it comes to every GUI-based tool, so by having yt function at hand, you can always call things like:

Bash
yt vs code configure snippets

Hard code aliases for frequently used pages

If you have any pages that you use on a daily bases, just hard code the in form of the aliases. For example, when I’m in the terminal, I often need to check some error code value of the UDS protocol (Unified Diagnostics Services). There’s a section on the UDS Wikipedia page for that, so I just have an alias uds in my .bashrc file for this purpose:

Bash
alias uds='google-chrome https://en.wikipedia.org/wiki/Unified_Diagnostic_Services#Negative_response_codes'

Adding # after the Wikipedia page name in the URL allows you to link directly to a page section, instead of the beginning of the page.