1. Introduction
Knowing the current weather conditions can be necessary for various reasons, such as planning outdoor activities, determining the best outfit for the day, or even deciding if travel is safe. While many websites and apps provide weather information, accessing the weather from the command line is a fast and efficient way to quickly check the current weather. In this tutorial, we’ll show how to check the weather from the command line using the curl and jq commands.
2. Using curl and jq
To check the weather from the command line, we’ll need to use two commands, curl and jq. curl is a command-line tool used to transfer data from or to a server, and jq is a command-line JSON processor.
The weather information can be obtained from various APIs such as OpenWeatherMap, Weather Underground, etc. In this example, we’ll use the OpenWeatherMap API.
First, we’ll separate out the curl
call and describe the parameters. The curl command makes a request to the following URL:
http://api.openweathermap.org/data/2.5/weather?q=<city_name>,<country_code>&appid=<our_api_key>
The parameters are:
- q: the city name and country code, separated by a comma. For example: San Francisco, US
- appid: our API key. We can sign up for a free API key on the OpenWeatherMap website.
Next, we’ll show the curl call and its output:
$ curl -s "http://api.openweathermap.org/data/2.5/weather?q=San Francisco,US&appid=<our_api_key>"
{
"coord": {
"lon": -122.42,
"lat": 37.77
},
"weather": [
{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03d"
}
],
...
}
Finally, we’ll show the jq piped version:
$ curl -s "http://api.openweathermap.org/data/2.5/weather?q=San Francisco,US&appid=<our_api_key>" | jq '.weather[0].description'
Output: “scattered clouds”
For more information on using curl for REST API calls, check out this article. To learn more about using jq to process JSON data, see this article.
3. Using wttr
Another way to get the weather from the command line is by using a command-line tool called wttr. wttr is a free, open-source tool that provides weather information in the terminal. To install wttr, we need to have a package manager such as Homebrew or apt-get installed.
Install Wttr using Homebrew:
$ brew install wttr
Install Wttr using apt-get:
$ sudo apt-get install wttr
Once Wttr is installed, we can use the following command to check the weather:
$ wttr <city_name>
Replace <city_name> with the name of the city we want to get the weather information for. For example, if we run the following command:
$ wttr San Francisco
Weather for San Francisco, CA, US:
⛈️ Light Rain
Temperature: 17.8°C Feels Like: 16.4°C
Wind: 16km/h N
Humidity: 93%
Pressure: 1015hPa
UV Index: 0 (Low)
wttr will provide a detailed weather report for the specified city, including the current temperature, wind speed, humidity, and more.
For more information, see the man page for wttr
4. Conclusion
In this article, we explained how to check the weather from the command line using the curl and jq commands or the wttr tool. By using these methods, you can quickly and easily retrieve current weather information for any city, saving you time and effort.
The curl and jq commands allow us to retrieve the weather information from an API such as OpenWeatherMap. The wttr tool provides a detailed weather report, including temperature, wind speed, humidity, and more.