A lightweight, simple C program to check and display the battery percentage of a Linux system.
oBattery reads battery information directly from Linux system files (/sys/class/power_supply/) and displays the current battery percentage. It's designed to be minimal, fast, and dependency-free.
- Lightweight and minimal dependencies
- Written in pure C
- Simple and accurate battery calculation
- Linux-only (uses
/sys/class/power_supply/) - Fast execution
- Easy to integrate into shell scripts or status bars
- Highly customizable output formats
- GCC compiler
- Linux system with battery support
- Standard C library (libc)
If you're using Arch Linux or an Arch-based distribution, you can install oBattery directly from the AUR:
Using yay:
yay -S obatteryUsing paru:
paru -S obatteryUsing makepkg:
git clone https://aur.archlinux.org/obattery.git
cd obattery
makepkg -sigcc -o obattery code.csudo install -Dm755 obattery /usr/local/bin/obattery./obatteryOr if installed system-wide:
obatterySimply execute the binary to display your current battery percentage:
obatteryobattery [options]Available options:
-p, --precision <num>- Set the number of decimal places (default is 2)-n, --number- Return only the numeric value (no text or % symbol)-h, --help- Show help message-v, --version- Show program version-a, --author- Show program author
Default output:
obattery
# Output: Battery: 85.50%Numeric value only (perfect for status bars):
obattery -n
# Output: 85.50With custom precision:
obattery -p 0
# Output: Battery: 86%Numeric with no decimals:
obattery -n -p 0
# Output: 86oBattery reads from the Linux power supply interface:
/sys/class/power_supply/BAT0/energy_current- Current battery energy/sys/class/power_supply/BAT0/energy_full- Full battery capacity
It calculates the percentage as: (current_energy / full_energy) × 100
For maximum customization, use the -n flag to get only the numeric value:
obattery -n -p 0This will output just the number (e.g., 85), allowing you to format it however you like in your status bar configuration.
Example with Polybar:
[module/battery]
type = custom/script
exec = obattery -n -p 0
interval = 5
format = <label>
format-prefix = "🔋 "
label = %output%%%Example with i3status:
# In your status bar script
echo "BAT: $(obattery -n -p 0)%"BATTERY=$(obattery)
echo "$BATTERY"Or for just the number:
BATTERY=$(obattery -n -p 0)
echo "Battery: $BATTERY%"Battery not detected:
- Verify
/sys/class/power_supply/exists on your system - Check if
BAT0is the correct battery device name (may vary by system) - Run
ls /sys/class/power_supply/to see available devices
GNU General Public License v3.0
Permissions of this strong copyleft license are conditioned on making available complete source code of licensed works and modifications, which include larger works using a licensed work, under the same license. Copyright and license notices must be preserved. Contributors provide an express grant of patent rights.
Contributions are welcome! Feel free to submit issues or pull requests.
spyflow
Made for Linux users.