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.
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
}
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
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 Source | Website | News Source | Website |
abc-news-au | ABC News (AU) | cnn | CNN |
ars-technica | Ars technica | espn | ESON |
associated-press | Associated Press | espn-cric-info | ESPN Cric Info |
axios | Axios | google-news | Google News |
bleacher-report | Bleacher Report | hacker-news | Hacker News |
bbc-news | BBC News | ign | IGN |
bbc-sport | BBC Sport | medical-news-today | Medical news Today |
bild | Bild | mtv-news | MTV News |
bloomberg | Bloomberg | national-geographic | National Geographic |
the-huffington-post | The Huffington Post | new-scientist | New Scientist |
buiseness-insider | Business Insider | new-york-magazine | New York magazine |
buzzfeed | Buzfeed | reddit-r-all | Reddit r/all |
wired | Wired | vice-news | Vice News |
time | Time | the-washington-post | The Washington Post |
the-wall-street-journal | The Wall Street Journal | the-verge | The Verge |
techradar | Techradar | reuters | Reuters |
the-hindu | The Hindu | the-telegraph | The Telegraph |
the-times-of-india | The Times of India | daily-mail | Daily mail |
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.