Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: variable outside function scope

Tags:

bash

Is there a possible way to initialize a global variable in bash and assign it a value in a function, then use it out side the function scope?

Function example:

globla_var=""

_DBINFO()
{
  curl  -su $AUTH https://<balla bla >/databases | jq -c 'map(select(.plan.name != "Sandbox")) | .[] | {id, name}'| \
  while  read db
  do
    idb=$(echo "$db" | jq -r '.id')
    name=$(echo "$db" | jq -r '.name')
    if [[ $name = '<bla>' ]]; then
        $global_var_her = $(<bla value>)
     fi
  done
}

then use it outside the function:

 echo $global_var

the result

   $0: line 16: =<bla bla>: command not found

I tried using declare:

declare -r global_var

same results

like image 937
DSD Avatar asked Oct 27 '15 19:10

DSD


People also ask

Can you access a variable outside of a function?

You can access such variables inside and outside of a function, as they have global scope. The variable x in the code above was declared outside a function: x = 10 . Using the showX() function, we were still able to access x because it was declared in a global scope.

Do Bash variables have scope?

Variables Scope In Bash, all variables by default are defined as global, even if declared inside the function. Local variables can be declared within the function body with the local keyword and can be used only inside that function. You can have local variables with the same name in different functions.

What is $_ in bash?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.

How do I make a variable global in bash?

In order to set a permanent environment variable in Bash, you have to use the export command and add it either to your “. bashrc” file (if this variable is only for you) or to the /etc/environment file if you want all users to have this environment variable.


1 Answers

Yes you can, but you have to be careful about subshells that limit scope in unexpected ways. Piping to a while read loop like you are doing is a common pitfall.

Instead of piping to a while read loop, use redirection and process substitution:

_DBINFO()
{     
  while read db
  do
    idb=$(echo "$db" | jq -r '.id')
    name=$(echo "$db" | jq -r '.name')
    if [[ $name = '<bla>' ]]; then
        global_var=value
     fi
  done  <  <(curl  -su "$AUTH" "https://$host/databases" |
               jq -c 'map(select(.plan.name != "Sandbox")) | .[] | {id, name}')
}

AUTH="user:password"
host="example.com"
_DBINFO
echo "The global variable is $global_var"

You also need to make sure your assignment is syntactically valid. $var = value is not a valid bash assignment, while var=value is. shellcheck can point out many things like that.

like image 67
that other guy Avatar answered Oct 07 '22 12:10

that other guy