Buoy
Buoy

Reputation: 855

How to access global var when local has been unset in Bash

vv=1
cc() { local vv=2; echo $vv; unset vv; echo "${vv}3"; }
cc
echo $vv

Gives:

2
3
1

I was expecting:

2
13
1

How can I access the global variable once a variable with the same name has been set local in a function?

Upvotes: 2

Views: 162

Answers (1)

Morten
Morten

Reputation: 654

I don't think you can. If it's an exported environment variable you can find it by reading the environment, but as far as a global variable masked by a local one, AFAIK you're out of luck. Check the contents of, and copy as necessary, before declaring your local variable.

Upvotes: 1

Related Questions