Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting global variable in bash

Tags:

linux

bash

shell

I have function where I am expecting it to hang sometime. So I am setting one global variable and then reading it, if it didn't come up after few second I give up. Below is not complete code but it's not working as I am not getting $START as value 5

START=0
ineer()
{
    sleep 5
    START=5
    echo "done $START"   ==> I am seeing here it return 5
    return $START
}
echo "Starting"
ineer &

while true
do
    if [ $START -eq 0 ]
    then
        echo "Not null $START"  ==> But $START here is always 0
    else
        echo "else $START"
        break;
    fi
    sleep 1;
done
like image 695
eswaat Avatar asked Apr 16 '15 17:04

eswaat


People also ask

How do you set a global variable in shell?

Global shell defined as: "You can copy old shell's variable to new shell (i.e. first shells variable to seconds shell), such variable is know as Global Shell variable." To set global varible you have to use export command.

Does bash have global variables?

Global variables can be used by all Bash scripts on your system, while local variables can only be used within the script (or shell) in which they're defined. Global variables are generally provided on the system by default and are mainly environment and configuration variables.

What is $1 and $2 in bash?

$1 - The first argument sent to the script. $2 - The second argument sent to the script.


2 Answers

You run inner function call in back ground, which means the START will be assigned in a subshell started by current shell. And in that subshell, the START value will be 5.

However in your current shell, which echo the START value, it is still 0. Since the update of START will only be in the subshell.

Each time you start a shell in background, it is just like fork a new process, which will make a copy of all current shell environments, including the variable value, and the new process will be completely isolate from your current shell.

Since the subshell have been forked as a new process, there is no way to directly update the parent shell's START value. Some alternative ways include signals passing when the subshell which runs inner function exit.

common errors:

export

export could only be used to make the variable name available to any subshells forked from current shell. however, once the subshell have been forked. The subshell will have a new copy of the variable and the value, any changes to the exported variable in the shell will not effect the subshell.

Please take the following code for details.

#!/bin/bash
export START=0
ineer()
{
    sleep 3
    export START=5
    echo "done $START"  # ==> I am seeing here it return 5
    sleep 1
    echo "new value $START"
    return $START
}
echo "Starting"
ineer &

while true
do
    if [ $START -eq 0 ]
    then
        echo "Not null $START" #  ==> But $START here is always 0
        export START=10
        echo "update value to $START"
        sleep 3
    else
        echo "else $START"
        break;
    fi
    sleep 1;
done
like image 190
Kun Ling Avatar answered Oct 19 '22 06:10

Kun Ling


The problem is that ineer & runs the function in a subshell, which is its own scope for variables. Changes made in a subshell will not apply to the parent shell. I recommend looking into kill and signal catching.

like image 32
Politank-Z Avatar answered Oct 19 '22 06:10

Politank-Z