Skip to content

Instantly share code, notes, and snippets.

@vmsh0
Last active July 25, 2020 20:30
Show Gist options
  • Save vmsh0/3c901821ffa9d0630ae4a8da12ecc7f4 to your computer and use it in GitHub Desktop.
Save vmsh0/3c901821ffa9d0630ae4a8da12ecc7f4 to your computer and use it in GitHub Desktop.
Script to quickly change the brightness value on Linux
#!/bin/sh
low_brightness_clamp=200 # you should probably change this depending on the brightness range of your driver
dev=$1
prm=$2
sys="/sys/class/backlight/$dev"
if [ ! -e "$sys" ]; then
>&2 echo "device not found"
exit 1
fi
[ -n "$prm" ] && [ "$prm" -eq "$prm" ] 2>/dev/null
if [ $? -ne 0 ]; then
>&2 echo "invalid input"
exit 2
fi
max_brightness=$(cat "$sys/max_brightness")
brightness=$(cat "$sys/brightness")
new_brightness=$(($brightness + $prm))
if [ $new_brightness -gt $max_brightness ]; then
new_brightness=$max_brightness
fi
if [ $new_brightness -lt $low_brightness_clamp ]; then
new_brightness=$low_brightness_clamp
fi
echo $new_brightness > "$sys/brightness"
ACTION=="add", SUBSYSTEM=="backlight", RUN+="/bin/chgrp video /sys/class/backlight/%k/brightness"
ACTION=="add", SUBSYSTEM=="backlight", RUN+="/bin/chmod g+w /sys/class/backlight/%k/brightness"
@vmsh0
Copy link
Author

vmsh0 commented Jul 25, 2020

You'd probably want to put the script in /usr/bin/brightness, and the udev rules in /etc/udev/rules.d/50-backlight.rules.

Then you should add your user to the video group with
usermod -aG [username]

Restart your PC to have the udev rules applied, then use as e.g.:

brightness intel_backlight +500
brightness intel_backlight -500

(you can find out what to put in the place of "intel_backlight" by looking in /sys/class/backlight/)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment