Overview
This article will explain how to write a bash script that changes the computer's brightness in Linux. This demonstration uses Ubuntu and the package "brightnessctl" was downloaded and used in this script. It is beneficial to write a script to change the brightness to accurately change it to any number from the brightness scale.
Materials
Computer
Linux Operating System
Terminal
- Packages that are needed to be downloaded: brightnessctl
Procedure
- In the terminal, download brightnessctl by using the command below:
sudo apt install brightnessctl
How "brightnessctl" Works in the Terminal
After downloading the package, running the command "brightnessctl -l" shows a list of the computer's devices that are labeled in single quotes or '' to change the brightness of that component. The 'intel_backlight' is changing the brightness of the computer screen.
To use the "brightnessctl" command in the terminal, it follows this order:
brightnessctl -d "name_of_device" set brightness_number
or
brightnessctl -d "name_of_device" set brightness_percentage
What the "brightnessctl" Command Means
brightnessctl -d: Necessary keyword to initiate the brightness
"name_of_device": The name of the device is in single quotes or ''where it can be retrieved after using the command "brightness -l". In this demonstration, 'intel_backlight' is the device whose brightness is being changed.
set: A keyword to change the brightness by either a number or a percentage
brightness_number or brightness_percentage: The "brightnessctl" scale takes a number from 0 to 19200, where 19200 is the highest number to produce maximum brightness. By giving any number from 0 to 19200, also outputs its percentage equivalent in the terminal. A percentage from 0% to 100% can also be specified instead and its brightness number equivalent is also returned.
If the brightnessctl command does not work, use sudo with it
Setting a Brightness Number
Below shows using the "brightnessctl" command by specifying numbers between 0 to 19200. By specifying a number, it shows its percentage equivalent.
Setting a Brightness Percentage
Below shows using the "brightnessctl" command by specifying percentages and it shows its brightness number equivalent.
Creating a Bash Script that uses "brightnessctl"
- Write the bash shebang in the first line of the script
#!/bin/bash
- Since the "brightnessctl" command can take either a number or a percentage to change the brightness, a variable was declared to either take a number or a percentage
#!/bin/bash
read -p "Choose '1' to enter a brightness number or choose '2' a percentage: " brightness_choice
echo "Option chosen: "$brightness_choice
An if and else statement block has been added afterward to see if the variable from the read condition was accepted properly. It is looking to see if either 1 or 2 is entered to give a number to change the brightness or a number to change the percentage respectively. If any other number has been inputted, then the script will have to be rerun.
The if and elif blocks have more read variables to take another input that would be used in the "brightnessctl" statement to change the brightness
Final Script
#!/bin/bash
read -p "Choose '1' to enter a brightness number or choose '2' a percentage: " brightness_choice
echo "Option chosen: "$brightness_choice
if [ $brightness_choice -eq 1 ]
then
read -p "Enter a number from 0 (minimum brightness) to 19200 (maximum brightness): " brightness_number
sudo brightnessctl -d "intel_backlight" set $brightness_number
elif [ $brightness_choice -eq 2 ]
then
read -p "Enter a number for the brightness percentage: " brightness_percentage
sudo brightnessctl -d "intel_backlight" set $brightness_percentage%
else
echo "Not a valid option. Try again."
fi
Source Code
https://github.com/AndrewDass1/SCRIPTS/tree/main/Linux/Bash/Change%20Brightness