Increment Brightness by value using xrandr

6,625

Copy bash script below to a file called bright

Then mark it executable with chmod a+x bright

Bash Script

#!/bin/bash

MON="DP-1-1"    # Discover monitor name with: xrandr | grep " connected"
STEP=5          # Step Up/Down brightnes by: 5 = ".05", 10 = ".10", etc.

CurrBright=$( xrandr --verbose --current | grep ^"$MON" -A5 | tail -n1 )
CurrBright="${CurrBright##* }"  # Get brightness level with decimal place

Left=${CurrBright%%"."*}        # Extract left of decimal point
Right=${CurrBright#*"."}        # Extract right of decimal point

MathBright="0"
[[ "$Left" != 0 && "$STEP" -lt 10 ]] && STEP=10     # > 1.0, only .1 works
[[ "$Left" != 0 ]] && MathBright="$Left"00          # 1.0 becomes "100"
[[ "${#Right}" -eq 1 ]] && Right="$Right"0          # 0.5 becomes "50"
MathBright=$(( MathBright + Right ))

[[ "$1" == "Up" || "$1" == "+" ]] && MathBright=$(( MathBright + STEP ))
[[ "$1" == "Down" || "$1" == "-" ]] && MathBright=$(( MathBright - STEP ))
[[ "${MathBright:0:1}" == "-" ]] && MathBright=0    # Negative not allowed
[[ "$MathBright" -gt 999  ]] && MathBright=999      # Can't go over 9.99

if [[ "${#MathBright}" -eq 3 ]] ; then
    MathBright="$MathBright"000         # Pad with lots of zeros
    CurrBright="${MathBright:0:1}.${MathBright:1:2}"
else
    MathBright="$MathBright"000         # Pad with lots of zeros
    CurrBright=".${MathBright:0:2}"
fi

xrandr --output "$MON" --brightness "$CurrBright"   # Set new brightness

# Display current brightness
printf "Monitor $MON "
echo $( xrandr --verbose --current | grep ^"$MON" -A5 | tail -n1 )
  • Change MON="DP-1-1" to your monitor name, ie MON="HDMI-1"
  • Discover your monitor names using xrandr | grep " connected"
  • Change STEP=5 to your step value, eg STEP=2 is less noticeable

Call the script with:

  • bright Up or bright + to increase brightness by step value
  • bright Down or bright - to decrease brightness by step value
  • bright (with no parameters) to get the current brightness level

Hopefully the bash / shell commands can easily be googled for education but if any questions don't hesitate to ask :)

8 minutes after posting answer it occurred to me I could have used bc for floating point math and saved ~10 lines of code and the a lot of time from the 1.5 hours to write it shrugs.

Share:
6,625

Related videos on Youtube

Fjolfrin
Author by

Fjolfrin

Updated on September 18, 2022

Comments

  • Fjolfrin
    Fjolfrin over 1 year

    So I have an Alienware 13 R3 with the OLED display, and for the first time I was able to change the brightness using the xrandr command. The problem was the absence of backlight in OLED displays, so I couldn't change the brightness wither with the keyboard, or in any other way. So now that I know I can change it, I want to put a key binding to change the brightness by let's say 0.1. I used this command to change the brightness:

    xrandr --output eDP-1-1 --brightness .5
    

    Does anyone know what command to use not to set the brightness, but to increase or decrease it by some value, so I can bind a macro to it. Thanks in advance!

    P.S. I am completely new to Linux, so please don't go hard on me :P

    • Admin
      Admin almost 5 years
      It would need to be scripted, there is no incrementing in xrandr. Also that is a software not hardware brigjhtness
    • Admin
      Admin almost 5 years
      @SergiyKolodyazhnyy I've just looked it up, and you're right. But I couldn't find any other way of changing the brightness.
    • Admin
      Admin almost 5 years
      Curious: since oled does not filter but has its autonomous color per pixel, is the screen really totally black when xrandr brihghtness is set to zero? If so, a sofware (scripted) solution would probably do. Please mention.
    • Admin
      Admin almost 5 years
      @JacobVlijm I can confirm, using the value 0 with this command, the screen turns 100% pitch OLED black.
    • Admin
      Admin almost 5 years
      Ah, then the mentioned dupe might help you out. If it doesn't, please shout.
    • Admin
      Admin almost 5 years
      @Jacob Vlijm when I run the commands on my terminal it works (!!) however not when I import the command on a shortcut. Since presumably I understood something wrong, in your solution I copy the "python3 <path_and_file name> up/down" to the "command" field in the key bind window, correct? imgur.com/a/bBWL0BD
    • Admin
      Admin almost 5 years
      Ah, I see, you need to set an absolute path, no tilde expansion. Well, you can, but not like this. Try absolute path, it'll work for sure.
  • Jeff Luyet
    Jeff Luyet over 3 years
    To complete this script, replace MON="DP-1-1" with MON=$(xrandr | grep " connected" | cut -f1 -d" "). This will detect your monitor automatically, otherwise you'll be editing this script on every reformat.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix over 3 years
    @JeffLuyet That's a great idea for single monitor systems. I have three monitors and many people have two monitors. Each monitor could have a different brightness and color temperature setting depending on sunrise and sunset transition periods. In the case of laptops you want the external monitor controlled by the script and not the built in monitor usually called eDP-1 these days. As such you can't automatically grab the first connected monitor reliably and it's best to hard code the external monitor name.
  • Admin
    Admin over 3 years
    Nice script! I simplified this for a script to toggle between bright and dim. I set DIM=0.3, then do NewBright=$(echo "-$CurrBright+1+$DIM" | bc).
  • Mario Awad
    Mario Awad over 2 years
    Thank you for the great script. I added [[ "$1" == "Reset" || "$1" == "0" ]] && MathBright=100 after your line [[ "$1" == "Down" || "$1" == "-" ]] && MathBright=$(( MathBright - STEP )) and this way I can pass Reset or 0 as argument to reset the brightness of the screen :-)