Alexey Kolesnikov


How to display time and battery percentage in the dwm bar

To change the default text in the right side of the dwm bar you need to set xsetroot name to whatever it is that you want. And usually dwm users choose to set time, date, battery percentage or internet connectivity.

I chose to display only the time and battery info in my bar. And here's my script for that.

#!/bin/bash
while true; do
# Get the current date and battery %
timebat=$(date +"%H:%M | $(cat /sys/class/power_supply/BAT0/capacity)% ")
# xsetroot to change the default name
xsetroot -name "$timebat"
# Update every 30 seconds, more than enough for time and battery info
sleep 30
done

Now you can run that script by changing it's permissions to allow execution.

chmod +x barscript.sh

and run

./barscript.sh

If everything is correct create a link for the script in the /bin directory.

ln barscript.sh /bin/barscript

And add the following line to the .xinitrc file that is in your home directory. Put it right before exec dwm.

barscript &
exec dwm

The "&" makes the script run in the background.

Now restart dwm and it should start the script.

Congrats! You've just put time and battery % in your dwm bar!

to index..