2

There is several command line web browsers, and although I am not yet a fond user of any of them, I started to like the experience of browsing the web from a simple command line from time to time.

A major annoyance is that most of the time spent of the web is done doing web search rather than browsing a page. The graphical browser I use most often, Opera, handles this well as the search bar asks like an interface for searches, I can type "g something" to search Google, "b something" to search Bing and "w something" to search Wikipedia for instance.

Is there an extention or plugin for command line web browsers, which allows doing a web search directly from the linux command line ? What I mean from the shell, I should be able to type for example :

program_name "words to search"

and be directly on the result page, instead of having to do that in two steps

lynx www.bing.com
[type my search there]

Note: Here I use lynx and Bing as examples, but it could be any search engine and any command-line browser.

3
  • 1
    What's wrong with Lynx? Try this: lynx http://www.google.com/search?q=search+from+commandline :) Basically, once you know the URL syntax for performing a search, you can call it directly.
    – Izzy
    Commented Jul 12, 2016 at 20:38
  • Wow, that's clever! I bet it's possible to create a simple shell script to build such made up URLs for you. Replacing spaces with '+' is the trickier part, though.
    – Bregalad
    Commented Jul 13, 2016 at 17:55
  • Sure it is. As you like the idea, find my answer below :)
    – Izzy
    Commented Jul 13, 2016 at 18:16

4 Answers 4

3

As Izzy point out, doing a web search directly on lynx works like a charm. However, if you are not comfortable with that solution, googler can do what you asked (but for searching google only). You will need to compile and install yourself, but instructions are given to that.

You can try surfraw too, it's available from repos (on debian systems at least). It can search multiple engines, so maybe it can better fulfill your requirements.

4

What's wrong with Lynx? Try this: lynx http://www.google.com/search?q=search+from+commandline :) Basically, once you know the URL syntax for performing a search, you can call it directly.

To make that easier, you can write up a tiny shell script (or alias), see How to urlencode data for curl command? on our sister site for a million interesting examples. As per our rule to include some essentials:

#!/bin/bash
url="http://www.google.com/search?q="
value="$*"
encoded_value=$(python -c "import urllib; print urllib.quote('''$value''')")
lynx ${url}${encoded_value}

This little snippet would require Python to be installed, which should not be a big issue on Linux systems. If you prefer Perl, or want to write up something in plain Bash: check the linked page on SO, you'll find code for almost everything. Here's one that would use only core stuff (which ships pre-installed with pretty much any Linux system):

#!/bin/bash
url="http://www.google.com/search?q="
value="$*"
encoded_value=$(echo "$1" | sed -e 's/%/%25/g' -e 's/ /%20/g' -e 's/!/%21/g' -e 's/"/%22/g' -e 's/#/%23/g' -e 's/\$/%24/g' -e 's/\&/%26/g' -e 's/'\''/%27/g' -e 's/(/%28/g' -e 's/)/%29/g' -e 's/\*/%2a/g' -e 's/+/%2b/g' -e 's/,/%2c/g' -e 's/-/%2d/g' -e 's/\./%2e/g' -e 's/\//%2f/g' -e 's/:/%3a/g' -e 's/;/%3b/g' -e 's//%3e/g' -e 's/?/%3f/g' -e 's/@/%40/g' -e 's/\[/%5b/g' -e 's/\\/%5c/g' -e 's/\]/%5d/g' -e 's/\^/%5e/g' -e 's/_/%5f/g' -e 's/`/%60/g' -e 's/{/%7b/g' -e 's/|/%7c/g' -e 's/}/%7d/g' -e 's/~/%7e/g')
lynx ${url}${encoded_value}

Save the variant you prefer as e.g. gsearch, make it executable, and there you go:

gsearch awful bash examples

:)

If you prefer a different search engine: https://duckduckgo.com/?q= would work for DuckDuckGo :)

1

Google

Check out these Python scripts which allow searching and downloading files from the command-line. It's based on the Google search results.

  • google_dl script (recommended).

    Usage:

    ./google_dl "search phrase"
    ./google_dl -s http://www.example.com/ -f pdf ""
    
  • gsrchDwn script (based on neo's script).

    Usage:

    ./gsrchDwn.py --query "search phrase"
    ./gsrchDwn.py --query "site:http://www.example.com/" --ftype pdf
    

Note: I'm the maintainer of both mentioned scripts.

Both of them are implementing xgoogle Python library. My fork of this library is based on the pkrumins/xgoogle version.

Other projects to consider:

  • googler

    A power tool to Google (Web & News) and Google Site Search from the command-line.


DuckDuckGo

Using DuckDuckGo via command-line:

  • Just go to: https://duckduckgo.com/tty/

    A command-line search engine implemented in JavaScript. No need to install anything.

  • Use ddgr utility, a cmdline utility to search DuckDuckGo from the terminal.

0

Hi, so you want to search from terminal using shell script?

Answer:

  • #!/usr/bin/env bash

  • clear

  • echo "Name of search engine"

  • echo -n "Search: "

  • read text

  • open "https://www.google.com/?q=$text"

  • exit 1

Are you happy, let me know....

Have a good day :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.