How to get battery status and level in a shell script

2021-07-10 3 min read

UPower is an abstraction for enumerating power devices, listening to device events and querying history and statistics. Using the upower command-line tool on GNU/Linux, you can get information about your power sources like the batteries your wireless mouse or laptop, and see their battery levels.

In this article, let's see how we can gather such information and use in a script.

How to use upower

First of all, list your power sources with the --dump, -d flag :

$ upower -d

If you have multiple power sources and want to show information about only one of them, you need to copy the device path starting with /org/freedesktop/UPower/devices of your target power source from the output of the previous command.

You need to pass that path using the --show-info, -i flag. For my laptop battery, I use the command below:

$ upower -i /org/freedesktop/UPower/devices/battery_BAT0

Or I could use the below command to only see the battery percentage:

$ upower -i /org/freedesktop/UPower/devices/battery_BAT0 | awk '/percentage/' {print $NF}'
87%

With awk, we can extract the desired information like above. Here's one another example with the grep command to list all the information at once:

$ upower -i $(upower -e | grep 'BAT') | grep -E "state|to\ (full|empty)|percentage"

Note that the above command is only good when you want to print some data in the terminal. For an advanced alternative, keep reading.

Extract and show battery information in a script

You may want to use the battery percentage or similar information in a shell script to make some automations or specific programs. If you have such goal, you can find a simple script I wrote below:

battery
#!/bin/bash

### Author: Eyüp Can ELMA
### Website: https://elma.dev
### This script is intended to be used to show information about power sources.

# YOU CAN EDIT THE LINES BELOW
DEFAULT_DEVICE_NAME='battery_BAT0'
# YOU CAN EDIT THE LINES ABOVE


# ! DO NOT EDIT VARIABLES BELOW ! #
DEVICE_NAME=${1:-'battery_BAT0'}
PATH_PREFIX='/org/freedesktop/UPower/devices'
DEVICE_PATH="$PATH_PREFIX/$DEVICE_NAME"
# ! DO NOT EDIT VARIABLES ABOVE ! #

if [ $# -gt 1 ]; then
  echo "Only one parameter is allowed."
  echo "Usage: '$0 <optional:device_name>'"
  exit 1
fi

# GATHER INFORMATION ABOUT THE BATTERY DEVICE
INFORMATION=$(upower -i "$DEVICE_PATH" 2> /dev/null)
PERCENTAGE=$(awk '/percentage/ {print $NF}' <<< $INFORMATION)
CAPACITY=$(awk '/capacity/ {print $NF}' <<< $INFORMATION)
UPDATED=$(awk -F '(' '/updated/ {print $NF}' <<< $INFORMATION | sed -r 's/\)//')
TIME_TO_EMPTY=$(awk -F ':' '/time to empty/ {print $NF}' <<< $INFORMATION | sed -r 's/\s{2,}//g')


print_info() {
  [ $# -ne 2 ] && echo "print_info() requires two parameters." && exit 1
  printf "%s\t%s\n" "$1" "$2"
}


print_info "percentage:" "$PERCENTAGE"
print_info 'capacity:' "$CAPACITY"
print_info 'updated:' "$UPDATED"
print_info 'time to empty:' "$TIME_TO_EMPTY"

You can change the default device name to the name of your power source by renaming the DEFAULT_DEVICE_NAME variable, or can pass that information to the script as an argument. Paste the codes above to a file named battery. With the chmod +x ./battery command, make it executable.

Usage:

$ ./battery
percentage:     37%
capacity:       75.4833%
updated:        44 seconds ago
time to empty:  2.8 hours
or
$ ./battery battery_BAT0
percentage:     92%
capacity:       84.1224%
updated:        46 seconds ago
time to empty:  5.9 hours

That's all.

Date: 2021-07-10
Categories: gnu linux