Get News Feed in the Linux Terminal

Read News In The Terminal

Terminal in Linux is a powerful tool. You can do anything in it, and when I say anything, I mean ANYTHING. One such cool thing you can do is get a news feed in the terminal. And it is extremely lightweight, considering we are only modifying our bashrc file to achieve this. Great, let’s get started!

Install curl and jq

We will need tools like curl and Jq to fetch feeds from a JSON API. Install these in your OS by typing the following commands in your Terminal, depending upon your distribution.

For Ubuntu-based systems:

sudo apt update && sudo apt -y install curl jq

For Arch Linux and Arch-based distributions:

sudo pacman -S curl jq

For Fedora Workstation:

sudo dnf install curl jq

Register on NewsAPI

In order to fetch news, we will have to get our own personal API first. Head over to NewsAPI’s official website and register yourself using an Email and a secure password.

Register On Newsapi
Register On NewsAPI

Once you’ve registered yourself, log in and head to home. From there, click on ‘Get API Key’. Copy the text string and paste it somewhere safe. Do not share this with anyone as this can be used to impersonate you. Now we’re ready to script.

Backup BashRC and Modify the script

First, let’s back up our current bashrc file in case of any mess-ups. Let’s make a copy of this file using the cp command:

cp ~/.bashrc ~/bashrc.bak

In the terminal, open the .bashrc file present in your Home directory in your preferred text editor (vim or nano) by typing the following commands :

vim ~/.bashrc

Or

nano ~/.bashrc

If you’re in Vim, press i to enter Insert mode. Navigate to the bottom and copy-paste the following script :

getnews () {
  curl https://newsapi.org/v2/top-headlines -s -G \
  -d sources=$1 \
  -d apiKey=YOUR_API_KEY \
   | jq -r '.articles[] | .title, .url, .description, ""'
  }
 
 topnews () {
   tput setaf 3; printf "News from the BBC:\n"
   tput setaf 6; getnews bbc-news
   tput setaf 3; printf "News from The Hindu:\n"
   tput setaf 6; getnews the-hindu
   tput setaf 3; printf "News from Time:\n"
   tput setaf 6; getnews time
   tput setaf 3; printf "Top News from India:\n"
   tput setaf 6; getnews the-times-of-india
 }
My custom Code for News (without the API key), it also updates my system.
My custom Code for News (without the API key), it also updates my system.

Replace the YOUR_API_KEY with your own API key that you’ve got earlier from the NewsAPI website.

Press Ctrl+O to save the file in Nano and press Ctrl+X to exit.

In the Vim text editor, press the escape key to enter visual mode and then type :wq to save and exit.

Streaming News in Linux Terminal

Now, either restart your Terminal or type the following command for bash to recognize our updated bashrc file:

source .bashrc

Finally, type this command in the Terminal and see the output:

topnews
Output Of The Topnews Command
Output Of The topnews Command

In the line | jq -r '.articles[] | .title, .url, .description, ""' you can also add the fields like .content, .author to get small content and the author’s name respectively, but I feel like it clutters the Terminal, so I didn’t add it to my code. You can also choose to remove the .url or .description if you feel that it clutters the Terminal, and thus you can just obtain the headlines. Don’t forget to separate those fields with a comma (,).

Press the Ctrl key and then click on the News URL to open the article in your Web Browser.

You can also choose to change the color of the output (specified using the tput setaf command in the code) by changing the numbers according to the chart mentioned in this article.

Next, you can also change the news source to get news from your preferred media outlet. NewsAPI offers a ton of News Websites to choose from. Add getnews <news-source> to the above code. The sources are :

News SourceWebsiteNews SourceWebsite
abc-news-auABC News (AU)cnnCNN
ars-technicaArs technicaespnESON
associated-pressAssociated Pressespn-cric-infoESPN Cric Info
axiosAxiosgoogle-newsGoogle News
bleacher-reportBleacher Reporthacker-newsHacker News
bbc-newsBBC NewsignIGN
bbc-sportBBC Sportmedical-news-todayMedical news Today
bildBildmtv-newsMTV News
bloombergBloombergnational-geographicNational Geographic
the-huffington-postThe Huffington Postnew-scientistNew Scientist
buiseness-insiderBusiness Insidernew-york-magazineNew York magazine
buzzfeedBuzfeedreddit-r-allReddit r/all
wiredWiredvice-newsVice News
timeTimethe-washington-postThe Washington Post
the-wall-street-journalThe Wall Street Journalthe-vergeThe Verge
techradarTechradarreutersReuters
the-hinduThe Hinduthe-telegraphThe Telegraph
the-times-of-indiaThe Times of Indiadaily-mailDaily mail
News sources of various webistes

We have only mentioned a few sources because there are lots of them. you can head over to https://newsapi.org/sources and copy-paste the ‘Source ID’ depending upon the country you live in.

Summary

You can also get the articles from individual sources independently by typing getnews <news-source> in the Terminal too. Hopefully, this article helped you in being up-to-date with the current news events of your country and the world.