Skip to content

Instantly share code, notes, and snippets.

@mb720
Created December 1, 2019 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mb720/45932d491f4e08b7313cab5f4925436b to your computer and use it in GitHub Desktop.
Save mb720/45932d491f4e08b7313cab5f4925436b to your computer and use it in GitHub Desktop.
Script for changing monitor brightness on Linux. Could be mapped to the XF86MonBrightnessUp and XF86MonBrightnessDown keys.
#!/usr/bin/env bash
# Changes the monitor brightness.
# Usage examples: "screen_brightness.sh +500", "screen_brightness 3000"
if (( $# >= 1 )); then
argument="$1"
# Writing to this file changes the monitor brightness.
# The parentheses are used to have a wildcard for the
# directory containing the brightness file. The directory
# could be "intel_backlight" or "acpi_video0", for example
brightness_file=(/sys/class/backlight/*/brightness)
if [[ $argument = +* ]]; then
amount=${argument#*+}
current_brightness=$(cat "${brightness_file}")
new_brightness=$((current_brightness + amount))
echo $new_brightness > "${brightness_file}"
elif [[ $argument = -* ]]; then
amount=${argument#*-}
current_brightness=$(cat "${brightness_file}")
new_brightness=$((current_brightness - amount))
echo $new_brightness > "${brightness_file}"
else
# If the argument is something else than an integer, this will
# yield "write error: Invalid argument"
echo "${argument}" > "${brightness_file}"
fi
else
echo "Please pass a single argument. Usage examples: \"screen_brightness.sh +500\", \"screen_brightness 3000\""
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment