Brightness Control for Laptops - a Solution

The last Lubuntu version that will control brightness using Fn+F9 and Fn+F10 is 20.04.
Later versions only accept going through the “Preferences/Brightness” menus, which is a major pain and unproductive.

I’ve found a solution to this, which is pretty universal as it controls gamma (as opposed to backlight). It will most likely also work on desktop machines etc. (not tested).
Brightness can be controlled in 10% steps from 20%…100%.

I attach two scripts, one for increasing and one for decreasing brightness.
Place them in /usr/bin with ownership root:root and permissions -rwxr-xr-x and attach them to Fn+F9 and Fn+F10 hotkeys respectively.

Works for me on 24.04 LTS.

Cheers.

PS: I tried to upload the files, but it’s forbidden. [So here you have them in the miserably formatted “blockquote” that’s allowed here. {issue solved}]
The first is for decreasing, the second for increasing brightness.

Decrease brightness:

#!/bin/bash
levelin=$(xrandr -q --verbose | grep -w "Brightness:" | tail -c 4)
ltmp=$(echo "scale=0; ($levelin*10)/1" | bc) 
if [ $ltmp -le 10 ] && [ $ltmp -gt 2 ]
then
	ltmp=$[ltmp-1]
else
	ltmp=$[2]
fi
levelout=$(echo "scale=1; $ltmp/10" | bc)
xrandr --output LVDS1 --brightness $levelout

#Uncomment for debug:
#echo $ltmp
#echo $levelout

Increase brightness:

#!/bin/bash
levelin=$(xrandr -q --verbose | grep -w "Brightness:" | tail -c 4)		#Get Current brightness
ltmp=$(echo "scale=0; ($levelin*10)/1" | bc) 							#Make it integer
if [ $ltmp -lt 10 ] && [ $ltmp -ge 2 ]									#Set limits: 0.2...1.0
then
	ltmp=$[ltmp+1]
else
	ltmp=$[10]
fi
levelout=$(echo "scale=1; $ltmp/10" | bc)
xrandr --output LVDS1 --brightness $levelout							#Set new brightness

#Uncomment for debug:
#echo $ltmp
#echo $levelout
1 Like

It can format better if instead of blockquote, type a line of only three backticks before and a line of only three backticks after each code snippet:

```
code here
```

Thanks. Not really super-intuitive.
I miss the old forum.

Other than what was suggested, the easiest way to add code tags is by highlighting the text and clicking on the </> icon in the composer.

Or use Ctrl + E on the keyboard.

If you take some time to explore the platform, you will discover many new and powerful features.

If you have any questions, let us know.

1 Like

Thank you for that! I hope to use it when I move over to a replacement for my desktop computer.

Is the number of increments hard set to 10, or is there any way to set the increment count to 20 ?

I’m no script master, so be gentle.
As it is, brightnees can be set to 100%…20% in 10% steps, which is fine for me.
Do you need finer granularity (eg, 5% steps) or fewer steps (eg 20% steps)?
It can be done.
But it’s all hardcoded, it’s a very primitive script.

I was wondering if the steps could be set to 5% (i.e. 20 increments, not increments of 20).

BTW, I already saved those two scripts for future use. :slight_smile:

I also changed the one line to the following:

xrandr -q --verbose | grep -w "Brightness:" | awk '{ print $NF }'

The use of “open double-quote” and “close double-quote” in yours didn’t work for me. I had to use the simple double-quote. Also, that “$NF” gives the last parameter only, without any extraneous characters like tab or space.

Thank you for responding. Much appreciated.



I’ve figured out how to get the 20 steps. Also modified the scripts a bit with functionally-labelled parameters. Here is my version of the two scripts:


Increment (HW_Admin__Laptop_FKBright_inc.sh):

#!/bin/bash

brightControl="LVDS1"
steps=20
minSteps=4

# Get Current brightness
levelin=$(xrandr -q --verbose | grep -w "Brightness:" | awk '{ print $NF }' )

# Make it integer
ltmp=$(echo "scale=0; (${levelin}*${steps})/1" | bc)

# Set limits: 0.2 … 1.0
if [ ${ltmp} -lt ${steps} ] && [ ${ltmp} -ge ${minSteps} ]
then
	ltmp=$[ltmp+1]
else
	ltmp=$[${steps}]
fi

levelout=$(echo "scale=1; ${ltmp}/${steps}" | bc)

# Set new brightness
xrandr --output ${brightControl} --brightness ${levelout}

#Uncomment for debug:
#echo $ltmp
#echo $levelout

Decrement (HW_Admin__Laptop_FKBright_dec.sh):

#!/bin/bash

brightControl="LVDS1"
steps=20
minSteps=4

# Get Current brightness
levelin=$(xrandr -q --verbose | grep -w "Brightness:" | awk '{ print $NF }' )

# Make it integer
ltmp=$(echo "scale=0; (${levelin}*${steps})/1" | bc)

# Set limits: 0.2 … 1.0
if [ ${ltmp} -lt ${steps} ] && [ ${ltmp} -ge ${minSteps} ]
then
	ltmp=$[ltmp-1]
else
	ltmp=$[${minSteps}]
fi

levelout=$(echo "scale=1; ${ltmp}/${steps}" | bc)

# Set new brightness
xrandr --output ${brightControl} --brightness ${levelout}

#Uncomment for debug:
#echo $ltmp
#echo $levelout

Thanks. I’ve edited my OP using “Formatted text” and it looks much better now.

2 Likes

To be honest, this is my first “real” script. As I said be gentle.
If awk works better for you than tail, great. The line simply extracts the x.xx Brightness value from xrandr.

For 5% increments, try the following (not tested):

Increase brightness:

#!/bin/bash
levelin=$(xrandr -q --verbose | grep -w "Brightness:" | tail -c 4)		#Get Current brightness
ltmp=$(echo "scale=0; ($levelin*20)/1" | bc) 							#Make it integer
if [ $ltmp -lt 20 ] && [ $ltmp -ge 4 ]									#Set limits: 0.2...1.0
then
	ltmp=$[ltmp+1]
else
	ltmp=$[20]
fi
levelout=$(echo "scale=1; $ltmp/20" | bc)
xrandr --output LVDS1 --brightness $levelout							#Set new brightness

#Uncomment for debug:
#echo $ltmp
#echo $levelout
1 Like

Yes, yours is much more professional and flexible.

1 Like

Thank you, but it is more about following a discipline regarding coding/formatting which then allows for later opportunity of “pattern recognition” (to remove ambiguity when trying to perform variable name-string identification) to “mass-massage”, if necessary, using stream editor (sed) or for code parsing using awk, in order to “fix” unexpected outcomes. Also, I try to keep distinct logic segments segregated with preample comments, rather than comments at end of line. That again simplifies for parsing scripts when you want to ignore anything having to do with comments in the files.

:slight_smile:

Did a bit of thinking here, and the more I look at your version compared to my primitive script with fixed constants, the more I like mine.
Yours is from a programming aesthetics view much nicer, but it opens up a big can of worms.
What if someone sets “steps” to 19? Or “minSteps” to 7? The script will invariably fail. The whole algorithm is based on integer arithmetic with very tight constraints.
Either you’ll need to write a novel in the comments, or devise some kind of error checking and reporting.
Your comment on using awk instead of tail I appreciate and use in the future, but the rest I’ll leave hardcoded. Much safer for other users.

1 Like

I’ve been doing some changes to my script to make it more useful. It’s still partially hardcoded, which is a conscious choice.
It’s now possible to set the brightness increments to 1%, 2%, 5%, 10% and 20%.
That should give enough flexibility.

Instead of two scripts, it’s now only one.

Command line execution:
“brightness” will output the current brightness level as 0.20…1.00
“brightness dec” will decrease brightness by X%
“brightness inc” will increase brightness by X%

I hope you find this helpful,

Cheers.

#!/bin/bash

# Bash script "brightness". Anders Bøcher, 2025 for Lubuntu versions 20.xx and later.

# This script controls display brightness by adjusting Gamma.
# It should be placed in:
# /usr/bin with ownership root:root and permissions -rwxr-xr-x
# It will de-/increase screen brightness over the range 20%...100%.
# Command usage:
# "brightness" will display the current brightness level as 0.20...1.00.
# "brightness dec" will decrease brightness by 1...20% (default 10%). Typically attached to hotkeys 'Fn+F9'.
# "brightness inc" will increase brightness by 1...20% (default 10%). Typically attached to hotkeys 'Fn+F10'.

# Select active display (typically "LVDS1", "DVI1", "VGA1", "TV1", "VIRTUAL1"...).
# Execute "xrandr" from command line to identify it.
currentdisplay="LVDS1"

# Set brightness step size in %. Allowed values are ONLY: 1, 2, 5, 10, 20
stepsize=10

# Get current brightness x.xx
levelin=$(xrandr -q --verbose | grep -w "Brightness:" | awk '{ print $NF }' )
# Make it integer
ltmp=$(echo "scale=0; ($levelin*100)/1" | bc) 

if [ "$1" = "inc" ]; then
	if [ $((ltmp+stepsize)) -le 100 ]; then
		ltmp=$[ltmp+stepsize]
	else
		ltmp=$[100]
	fi
#	echo "increment"

elif [ "$1" = "dec" ]; then
	if [ $((ltmp-stepsize)) -ge 20 ];	then
		ltmp=$[ltmp-stepsize]
	else
		ltmp=$[20]
	fi
#	echo "decrement"

else
	echo "Current Brightness:" $levelin
fi

levelout=$(echo "scale=2; $ltmp/100" | bc)
xrandr --output $currentdisplay --brightness $levelout

#Uncomment for debug:
#echo $ltmp
#echo $levelout
#echo $levelin

1 Like

Continuing the discussion in this thread:

that I am not able to edit it any more, I’d like to present my final solution for the non-functional CLI brightness control. A few issues emerged that have been solved, eg, the display name.
The script is now fully functional and works with all allowed brightness steps.
It should be self-explaining.

Cheers.


#!/bin/bash

# Bash script "brightness", Anders Bøcher, 2025.
# XRandR must be version 1.2 or later

# This script controls display brightness by adjusting Gamma.
# It will de-/increase screen brightness over the range 20%...100%.

# The script should be placed in:
# /usr/bin with ownership root:root and permissions -rwxr-xr-x

# Command usage:
# "brightness" will display the current brightness level as x.xx
# "brightness dec" will decrease brightness by 1...20% (default 10%). Typically attached to hotkey 'Fn+F9'.
# "brightness inc" will increase brightness by 1...20% (default 10%). Typically attached to hotkey 'Fn+F10'.

# Set brightness step size. The ONLY valid values are: 1, 2, 5, 10, 20
stepsize=10

# Find active display (LVDS1, VGA-1, etc.)
currentdisplay=$(xrandr -q --verbose | grep -w "connected primary" | awk '{ print $1 }' )

# Get current brightness x.xx
levelin=$(xrandr -q --verbose | grep -w "Brightness:" | awk '{ print $NF }' )
# Make it integer
ltmp=$(echo "scale=0; ($levelin*100)/1" | bc) 

if [ "$1" = "inc" ]; then
	if [ $((ltmp+stepsize)) -le 100 ]; then
		ltmp=$[ltmp+stepsize]
	else
		ltmp=$[100]
	fi
#	echo "increment"

elif [ "$1" = "dec" ]; then
	if [ $((ltmp-stepsize)) -ge 20 ]; then
		ltmp=$[ltmp-stepsize]
	else
		ltmp=$[20]
	fi
#	echo "decrement"

else
	echo "Current Brightness:" $levelin
	exit
fi

levelout=$(echo "scale=2; $ltmp/100" | bc)
xrandr --output $currentdisplay --brightness $levelout

#Uncomment for debug:
#echo $currentdisplay
#echo $ltmp
#echo $levelout
#echo $levelin

What are you trying to edit?

FYI: I’ve not responded, as I have a number of devices where I have Lubuntu installed, and the laptop brightness keys operate as I expect them to on all releases; ie. your issue is likely device specific

PM me and I (or another moderator) maybe able to help.

I was trying to edit my last message in the original thread, and as this didn’t work, I made a new one. Brainfart from my side, sorry. Thanks for merging.

And no, it’s not a device issue. Same machine. Brightness control works fine until 20.04, all later versions don’t work. This is true for all five HP/Compaq laptops I’m running. And I’m not alone, it seems.

Just bear in mind that this isn’t a place to post personal notes, so I highly recommend coming up with your solution prior to posting.

I understand your message, but no, I’m not “thinking on the screen”.
The changes were based on feedback from other users. It unfortunately got a bit out of hand. I could delete some previous posts, but am afraid that it would make everything a mess, so rather not.

1 Like