Generic Screen Backlight-Toggling Shell Script

I’ve written a shell script to toggle my LCD backlight (aka, screen brightness). Currently it works on a Dell D505 laptop and Fujitsu V1040. Here it is:

# change backlight settings based on system
# symlink to /usr/bin/brightness, and call with "sudo brightness"
# make sure to disable password prompt for it with "sudo visudo"

case $HOST in
    aether) # Fujitsu V1040
        # since xbacklight always returns a single floating point number, we need to
        # convert it to an integer
        if [[ $int -eq 0 ]]; then
            xbacklight -set 100
            xbacklight -set 0
    luxion) # Dell Latidude D505
        b=$(cat /sys/class/backlight/dell_backlight/actual_brightness)
        let "bi=$b"
        if [[ $b -lt 7 ]]; then
            # gradually raise backlight, just like xbacklight
            while [[ bi -lt 7 ]]; do
                let "bi=$bi+1"
                echo $bi > /sys/class/backlight/dell_backlight/brightness
            while [[ bi -gt 0 ]]; do
                let "bi=$bi-1"
                echo $bi > /sys/class/backlight/dell_backlight/brightness

# vim:syntax=zsh

The script pretty simple. It looks at the name of the computer ($HOST environment variable), and then chooses the appropriate way to change the backlight (either gradually set it to 100 or 0). Since the neat xbacklight command doesn’t work on my Dell laptop, I’ve written my own method. This method of echo-ing the desired brightness number into a system file (yes, Linux is cool like that) seems to be the trend in the various Linux forums out there. The trick is to find the right “brightness” file. Some systems use /proc/acpi/video/…/brightness and others, like my Dell Latitude D505, in /sys/class/backlight/…/brightness (though, on my Dell, I have to read separately from the file “actual_brightness,” but you get the idea). The script is, very straightforward and easily modifiable to work on your own laptop.

The only requirement is that, at least when echo-ing anything into /proc or /sys, you need root privileges. So you have to run “sudo path/to/”. I’ve simplified the process as follows:

  • sudo ln -s path/to/ /usr/bin/brightness -> This makes the command “brightness” become available from anywhere in the system (since /usr/bin is in your $PATH environment variable).
  • sudo visudo -> Then add the line “your_username your_hostname=NOPASSWD: /usr/bin/brightness” -> This makes it so that typing “sudo brightness” does not require any password.
  • bind “sudo brightness” to a hotkey (consult your window manager) -> For me, I use SHIFT + CAPSLOCK + Backslash (capslock is my modkey in Xmonad).

Again, all of this “sudo visudo” and “sudo brightness” business is only required because my aging Dell laptop doesn’t play well with xbacklight.

So there you have it. A simple script that relies on either xbacklight, or a customized method, depending on the system. If you have multiple laptops that are supported by xbacklight, simply replace “aether” in the script above with “system1|system2|system3|sytem4” to save yourself from having to copy/paste the entire block of code for each system.