Check Weather Like a Pro: A Simple Bash Script for Your Terminal

Have you ever wanted to check the weather forecast directly from your terminal? In this blog, I'll show you how to create a simple Bash script that fetches weather details for any city using the free wttr.in
weather service.
Why Use a Bash Script for Weather?
Instead of opening a browser and searching for the weather, you can get instant results with a single command. This script is lightweight, fast, and efficiently automates weather checking.
Prerequisites
Before running the script, ensure you have:
A Unix-based system (Linux/macOS)
curl
installed (most systems have it by default)
The Bash Script
Bash script to fetch weather details:
#!/bin/bash
# Function to display usage
show_usage()
{
echo "Usage: $0 <city_name>"
echo "Example: $0 Delhi"
}
# Check if city is provided
CITY="$1" # Gets city as arrgument.
if [ -z "$CITY" ]; # City should not be empty.
then
echo "Error: City name not provided."
show_usage # Show the usage/guide the user.
exit 1
fi
# Displaying the loading message
echo "Fetching weather details for $CITY..."
# Fetching weather data for a city
WEATHER=$(curl -s "https://wttr.in/${CITY}?format=3") # fetch weather data from wttr.in in a simple text format.
# Checking if the response is valid
if [[ "$WEATHER" == *"Unknown location"* ]]; # If the city name is invalid, it displays an error message
then
echo "Error: Unable to fetch weather details for '$CITY'. Please check the city name."
exit 1
fi
# Display the weather details
echo "====================================="
echo " WEATHER REPORT "
echo "====================================="
echo " $WEATHER"
echo "====================================="
# Suggest viewing the full report
echo
echo "NOTE: For more details, visit: https://wttr.in/${CITY}"
echo
How It Works
The script takes a city name as an argument.
It uses
curl
to fetch weather data from wttr.in in a simple text format.If the city name is invalid, it displays an error message.
The script presents a formatted weather report directly in the terminal.
It also provides a link to view the full weather report online.
Running the Script
Save the script as weather.sh
, then provide execution permissions:
chmod +x weather.sh
Now, you can check the weather for any city:
./weather.sh London
Example Output
Fetching weather details for London...
=====================================
WEATHER REPORT
=====================================
London: ๐ฆ +9ยฐC
=====================================
NOTE: For more details, visit: https://wttr.in/London
Enhancements & Customization Ideas
Add Color: Use
tput
or ANSI escape codes to enhance readability.Auto-detect Location: Use
curl -s ipinfo.io/city
to fetch the user's current city.Integrate Notifications: Display alerts using
notify-send
on Linux orosascript
on macOS.
Conclusion
With just a few lines of Bash scripting, you can create a handy weather-checking tool. This script can be further customized to suit your needs. Try it out and let me know how you improve it!
Happy scripting! ๐
Subscribe to my newsletter
Read articles from Omkar Mahandule directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Omkar Mahandule
Omkar Mahandule
I am an aspiring DevOps & Cloud Engineer with a strong drive to learn, grow, and contribute to the world of automation, cloud computing, and scalable infrastructure. Passionate about automation, cloud computing, and scalable infrastructure. With a strong foundation in Linux, Git/GitHub, CI/CD, Docker, Kubernetes, and AWS, I have already completed the core phases of my DevOps journey and am now advancing into Infrastructure as Code (Terraform, Ansible), Monitoring, and DevSecOps. ๐ป What I Bring to the Table: ๐ง Linux & Scripting โ Shell scripting, process management, automation. ๐ฑ Version Control โ Git, GitHub/GitHub Actions, branching strategies. โก CI/CD Pipelines โ Jenkins, GitHub Actions, CI/CD. ๐ณ Containers & Orchestration โ Docker, Kubernetes (Minikube, EKS). โ๏ธ Cloud Computing โ AWS (EC2, S3, IAM, RDS, VPC, Load Balancers). ๐ฏ Next Steps in My DevOps Journey: ๐๏ธ Mastering Infrastructure as Code (Terraform, Ansible) for automated provisioning. ๐ Learning Monitoring & Observability (Prometheus, Grafana, AWS CloudWatch, ELK Stack). ๐ Exploring DevSecOps by implementing security scanning and best practices in CI/CD. ๐ก๏ธ Deepening my expertise in Kubernetes security (RBAC, Network Policies).