Build a CLI Weather App in Bash with curl and jq

Build a CLI Weather App in Bash with curl and jq

Build a CLI Weather App in Bash with curl and jq

 

Getting real-time weather information straight from the command line is not only fun but also a great way to learn about API integration, JSON parsing, and shell scripting. In this tutorial, we’ll build a simple yet effective weather application in Bash using curl to fetch data from the OpenWeatherMap API and jq to parse the JSON responses. By the end, you’ll have a customizable CLI tool that can show you current weather conditions in any city of your choice.

1. Prerequisites: Tools and Setup

Before diving into the code, make sure you have the following installed on your system:

  • Bash: Usually pre-installed on UNIX-like systems.
  • curl: For making HTTP requests to the OpenWeatherMap API.
  • jq: A lightweight and flexible command-line JSON processor.

You also need to create an account at OpenWeatherMap and generate a free API key.

2. Fetching Weather Data Using curl

OpenWeatherMap provides a simple HTTP API to get current weather data. The general format of a request looks like this:

https://api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}&units=metric

Here’s how to use curl to fetch the data for a given city:

#!/bin/bash

API_KEY="your_api_key_here"
CITY="London"

RESPONSE=$(curl -s "https://api.openweathermap.org/data/2.5/weather?q=$CITY&appid=$API_KEY&units=metric")
echo $RESPONSE

The -s flag tells curl to run in silent mode, which suppresses progress and error messages.

3. Parsing JSON with jq

Now that we have the raw JSON data, it’s time to extract readable information such as temperature, weather description, and humidity. Here’s how you can use jq for that:

echo $RESPONSE | jq '.main.temp'

This command retrieves the temperature. Let’s extend it for more fields:

echo "Temperature: $(echo $RESPONSE | jq -r '.main.temp') °C"
echo "Weather: $(echo $RESPONSE | jq -r '.weather[0].description')"
echo "Humidity: $(echo $RESPONSE | jq -r '.main.humidity')%"

The -r option in jq outputs raw strings, which is better for shell scripting.

4. Putting It All Together: The Full Script

Here’s a full implementation of the CLI weather app:

#!/bin/bash

API_KEY="your_api_key_here"
CITY="$1"

if [ -z "$CITY" ]; then
  echo "Usage: $0 "
  exit 1
fi

RESPONSE=$(curl -s "https://api.openweathermap.org/data/2.5/weather?q=$CITY&appid=$API_KEY&units=metric")

if echo "$RESPONSE" | jq -e .main > /dev/null; then
  echo "Weather in $CITY:"
  echo "Temperature: $(echo $RESPONSE | jq -r '.main.temp') °C"
  echo "Description: $(echo $RESPONSE | jq -r '.weather[0].description')"
  echo "Humidity: $(echo $RESPONSE | jq -r '.main.humidity')%"
  echo "Wind Speed: $(echo $RESPONSE | jq -r '.wind.speed') m/s"
else
  echo "Error fetching weather data. Make sure the city name is correct."
fi

Make your script executable with chmod +x weather.sh and run it like so:

./weather.sh Tokyo

This approach includes basic error handling, such as checking if the city name is valid.

5. Tips, Optimization, and Next Steps

Here are some improvements and ideas for extending your CLI weather app:

  • Environment Variables: Store your API key in a secure .env file or use ~/.bashrc to export it, avoiding hardcoding keys.
  • Caching: Avoid hitting the API repeatedly by caching the last result for a few minutes using touch and stat.
  • Location by IP: Use an IP geolocation API to automatically detect the user’s current city.
  • Pretty Output: Use colors with tput or ANSI codes to highlight temperature and warnings.
  • Integration: Combine with crontab or i3/swaybar scripts to show weather in your status bar.

By building this CLI utility, you’re not only scripting for automation but also learning how to interact programmatically with external services, improving both your Bash and web API skills.

Conclusion

Creating a Bash-based weather app using curl and jq is an enriching side project that combines practical knowledge from multiple domains: shell scripting, API consumption, and JSON parsing. It’s fast to set up, customizable, and serves as a useful tool you can use and build upon daily.

 

Useful links: