:battery: Bash script to print your Linux computer's battery level.
A Bash program to print your Linux machine’s battery level.
curl -O https://cszach.github.io/battery/battery
chmod +x battery
mv battery ~/.local/bin # Optional
battery --help
battery
BAT1
battery BAT1
battery --list
battery --color=always
BAT1
and colorize only if it is charging
battery --color=charging BAT1
75%
echo $?
0
: Successful.1
: Invalid option.2
: Battery could not be found.3
: Battery’s level could not be accessed./sys/class/power_supply
./sys/class/power_supply/BATTERY/capacity
whereBATTERY
is a power source detected by your Linux system./sys/class/power_supply/BATTERY/status
. TheCharging
, Discharging
, Full
, orUnknown
.I didn’t want to use a graphical desktop on my Parabola GNU/Linux-libre
laptop so I needed a way to know the battery level in the command line. I wanted
the output to be colorized, too (e.g. green for good battery level, red for weak
battery), so it was more than just a one-liner. Ultimately, I figured out I
could share this little Bash script with the Internet, so I decided to support
user-given power source and push this onto GitHub.
You can easily integrate this program with your PS1
command prompt, like this:
# ~/.bashrc
PS1="$(battery) $"
This work is licensed under the Creative Commons Zero v1.0 Universal
and is therefore a dedication to the public domain. This means you can copy,
modify, and redistribute the program without asking for permission.