Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assign local variable from function in linux bash a new value

Tags:

I have a linux bash script with a function:

myfunctiona ()  {   local MYVAR1="one"   local MYVAR2="two"   echo $MYVAR1   # The line beneath is the line in question!   local MYVAR1=$MYVAR1$MYVAR2        } 

When I want to give the LOCAL variable MYVAR1 in the function myfunctiona a new value, do I have to write

local MYVAR1=$MYVAR1$MYVAR2 

or can I also write

MYVAR1=$MYVAR1$MYVAR2 

With the second line without "local" do I create a global variable with the same name?

like image 415
Wolfgang Adamec Avatar asked Mar 20 '14 08:03

Wolfgang Adamec


People also ask

How do you set local variables in bash?

The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.

How will you declare a local variable in a function in Linux?

In a function, a local variable has meaning only within that function block. #!/bin/bash # Global and local variables inside a function. func () { local loc_var=23 # Declared as local variable. echo # Uses the 'local' builtin.

How do you set a variable value in bash?

To create a variable, you just provide a name and value for it. Your variable names should be descriptive and remind you of the value they hold. A variable name cannot start with a number, nor can it contain spaces. It can, however, start with an underscore.


2 Answers

Once you've defined a local variable you can assign it normally, like this:

#!/bin/bash  myfunctiona ()  {   local MYVAR1="one"   local MYVAR2="two"   echo $MYVAR1   # The line beneath is the line in question!   local MYVAR1=$MYVAR1$MYVAR2       MYVAR1="FOO"   echo $MYVAR1    }  myfunctiona echo "global" $MYVAR1 

which gives the output:

one FOO global 
  • As you can see attempting to access the variable from global scope returns null

HTH

like image 114
Geoff Williams Avatar answered Sep 27 '22 22:09

Geoff Williams


The correct way to do it would be:

MYVAR1="${MYVAR1}${MYVAR2}" 

The braces are usually used when you concatenate variables. Use quotes.

The variable is still local since you reassigned its value within the scope of the function. An example:

#!/usr/bin/env bash  _myFunction() {     local var_1="one"     local var_2="two"     local -g var_3="three" # The -g switch makes a local variable a global variable     var_4="four" # This will be global since we didn't mark it as a local variable from the start      var_1="${var_1}${var_2}"      echo "Inside function var_1=${var_1}"     echo "Inside function var_2=${var_2}"     echo "Inside function var_3=${var_3}"     echo "Inside function var_4=${var_4}" }  _myFunction  echo "Outside function var_1=${var_1}" echo "Outside function var_2=${var_2}" echo "Outside function var_3=${var_3}" echo "Outside function var_4=${var_4}" 

This results in:

$ ./script Inside function var_1=onetwo Inside function var_2=two Inside function var_3=three Inside function var_4=four Outside function var_1= Outside function var_2= Outside function var_3=three Outside function var_4=four 
like image 25
Saucier Avatar answered Sep 27 '22 21:09

Saucier