0

I'm currently writing a script which uses a variable i created via terminal by typing the commend:

export NUM_brightness=10

Now the script is attempting to increase the variable by 1, By doing:

NUM_brightness=$((NUM_brightness+1))

And just in case i also added

export NUM_brightness=$NUM_brightness

However when i run the script again the variable NUM_brightness does not increased and even stays the same as the first time i typed in the terminal: export NUM_brightness=10
Meaning it keeps NUM_brightness with value of 11 instead increasing it.

My question is how do i make the bash script to update the variable NUM_brightness so every time i run the script NUM_brightness will keep on increasing?

Here is the script:

NUM_brightness=$((NUM_brightness+1))
echo $NUM_brightness
NUM_temp="0.$NUM_brightness"
echo $NUM_temp
if [ "NUM_brightness" >=11 ]; then
  NUM_brightness=1
  NUM_temp=1
fi
export NUM_brightness=$NUM_brightness;
echo $NUM_brightness
8
  • Because a child sub process can't change environnement of it's parent like this Commented Apr 29, 2023 at 20:21
  • @GillesQuénot Can you please tell me how to then? Commented Apr 29, 2023 at 20:27
  • if you were to source the script you could cause the update to persist in the parent process; instead of executing the script (script_name) you source the script (. script_name or source script_name) Commented Apr 29, 2023 at 20:34
  • another option would be to convert the script into a function, have the function loaded by your login script (eg, .bashrc, .profile) and then call the function, which just happens to look like a script invocation (eg, script_name vs function_name) Commented Apr 29, 2023 at 20:36
  • 1
    @Andreasdetestscensorship Thanks and sorry, i just started to improve my stack overflow questions writing. Is it acceptable for me to answer my own question and mark it as solved? Commented Apr 30, 2023 at 5:49

3 Answers 3

2

Your script runs in a different shell from the one that you set your initial value in. When the shell that runs the script ends, all the variables of that shell are discarded.

Export marks variables and functions that are exported to child processes. Export does not export variables to parent processes.

Example:

action                       shell 1 (parent)             shell 2 (child)
-----------------------------------------------------------------------------
export NUM_brightness=10     NUM_brightness=10
script started               NUM_brightness=10            NUM_brightness=10
  script increments          NUM_brightness=10            NUM_brightness=11
  script exports             NUM_brightness=10            NUM_brightness=11
  script ends                NUM_brightness=10

If you want to increment the value in your current shell, you need to run the script in your current shell:

. script.sh

or

source script.sh
Sign up to request clarification or add additional context in comments.

Comments

0

note When you use a sub-shell, like using a script, you can't change the parent environnement.

What I would do with modern bash:

#!/bin/bash

((num_brightness++))
echo $num_brightness
num_temp="0.$num_brightness"
echo $num_temp
if ((num_brightness >=11)); then
    num_brightness=1
    num_temp=1
fi
export num_brightness=$num_brightness

Then:

$ source script.sh     # or     . script.sh
$ echo $num_brightness
2

if [ "NUM_brightness" >=11 ]; then

is plain wrong. NUM_brightness is treated as a string, not as a variable. Better use :

((...)) and $((...)) are arithmetic commands, which returns an exit status of 0 if the expression is nonzero, or 1 if the expression is zero. Also used as a synonym for "let", if side effects (assignments) are needed.

See http://mywiki.wooledge.org/ArithmeticExpression


[[ is a bash keyword similar to (but more powerful than) the [ command. See http://mywiki.wooledge.org/BashFAQ/031 and http://mywiki.wooledge.org/BashGuide/TestsAndConditionals. Unless you're writing for POSIX sh, I recommend [[

Comments

0

Thanks guys you helped me a lot, Here i posted what i was trying to accomplished:

Here are two scripts to increase or decrease respectively, The brightness using the xrandr tool.

USE source SCRIPT_NAME.sh to run both scripts.

REMEMBER to add export NUM_brightness=8 in the end of the .bashrc file, In order to make the script work.

Script 1 (Increase brightness):

#!/usr/bin/bash/
NUM_brightness=$((NUM_brightness+=1))
NUM_temp="0.$NUM_brightness"
if [[ NUM_brightness -ge 10 ]] then
  NUM_brightness=10
  NUM_temp=1
fi
echo "NUM_brightness >> $NUM_brightness \n NUM_temp >> $NUM_temp"
xrandr --output eDP-1 --brightness "$NUM_temp"

Script 2 (decrease brightness):

#!/usr/bin/bash/
NUM_brightness=$((NUM_brightness-=1))
NUM_temp="0.$NUM_brightness"
if [[ NUM_brightness -le 0 ]] then
  NUM_brightness=0
  NUM_temp=0.05
fi
echo "NUM_brightness >> $NUM_brightness \n NUM_temp >> $NUM_temp"
xrandr --output eDP-1 --brightness "$NUM_temp"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.