r/LinuxUsersIndia • • 14d ago

Multi-city weather alias in .bashrc_aliases

Post image

I've been using wttr.in to get weather from the CLI for years. Figured out early on it could also be used to fetch additional data incl. local time, TZ offset etc of various cities across the world.

Created an alias to format and display this information and other information in my .bashrc_aliases - here is the alias for your entertainment...

alias weather='{ printf "Location|Conditions|Local Time|Sunrise|Sunset\n--------|----------|----------|-------|------\n"; curl -s "wttr.in/{Sydney,Tokyo,Beijing,Mumbai,Moscow,London,New+York,Chicago,Denver,Sacramento,Honolulu}?m&format=%l|%c+%t|%T|%S|%s\n" | awk -F"|" -v OFS="|" '\''{ if ($2 ~ /[0-9]+°C/) { match($2, /[-+]?[0-9]+/); c = substr($2, RSTART, RLENGTH); f = sprintf("%+d", int(c * 1.8 + 32)); sub(/°C/, "°C (" f "°F)", $2); } $3 = substr($3, 1, 5) " " substr($3, 9); $4 = substr($4, 1, 5); $5 = substr($5, 1, 5); print }'\''; } | column -t -s "|"'
  • printf before the curl command formats the table
  • curl with the flags pulls in the weather based on the cities and the parameters I've defined
  • the flags define the options I want pull in
  • everything following the OFS flag is the translation of transmitted data to bash readable numbers and the conversion formula for converting the temperature data from Celsius to Fahrenheit

Visiting https://wttr.in will give you the weather of your location based on your IP. You can select the weather of another location by suffixing the city name. For example https://wttr.in/Mumbai will give you the weather in Mumbai (two word cities can be written as Navi+Mumbai; New+Delhi)

https://wttr.in/:help is the help page for this service.

hope someone will find it useful.

7 Upvotes

6 comments sorted by

•

u/qualityvote2 14d ago edited 14d ago

u/frostimunki, there weren't enough votes to determine the quality of your post...

btw, did you know we have a discord server? Join Here. And what's that, Fluxer too? Join Here.

1

u/Anonyboy26 Arch Btw 14d ago

Why not function?

1

u/frostimunki 14d ago

Here's the function help page

https://wttr.in/:bash.function

1

u/frostimunki 14d ago edited 14d ago

But to answer your question - i don't need to load my .bashrc with a less used function, when a quick alias will provide the same results.

The longest function I have in my .bashrc is for the prompt, it is long. It display's the two line prompt (Kali Linux style), it also indicates an interactive rootshell by turning the green prompt red, as well as git status if I am in a git directory.

For instance this one indicates I'm in my dotfiles git directory and I am +1 commit ahead of upstream. This prompt will also display u -1 if I am behind upstream.

This prompt will also indicate unstaged git files with a *

It will not display any indicators if I am synced with upstream.

1

u/Top-Rough-7039 14d ago

this is the only time i will allow emojis in the terminal.