Accessing local variable

Hi,

Would like to know the purpose and accessing of local variable as in below code snippet:

a=123
( a=321; )	      

echo "a = $a"   #This will print 123

How to access local a variable which is assigned with value 321 ?. ..

The short answer: Like this, you can't.

From the BASH man-page:

(list) list  is executed in a subshell environment
a=123
( a=321;
  echo "a = $a"   #This will print 321
)

Okay. ..

Any purpose behind it?. .. Or it is like a dummy value assigned to some orbitery variable?.

Thank you.

purpose willbe, If you want to do some calculation on same variable which is used by parent shell and don't want to replace the value of parent shell variable.

1 Like