On Mon, Feb 12, 2018 at 3:23 PM, Nikolai Kondrashov <spbn...@gmail.com> wrote:
> >> Take a look at these links: >> >> - http://wiki.bash-hackers.org/commands/builtin/unset >> - http://www.fvue.nl/wiki/Bash:_Passing_variables_by_reference >> > > Thanks, Clark! However, I find it difficult to follow the explanation at > the > first link and even more difficult to connect it to this case. So I'm not > sure > I'm getting it right. Could you maybe explain what exactly happens in the > code > I posted? > Your code: inner() { unset res if [[ $1 == "set" ]]; then res[0]="X" res[1]="Y" fi } outer() { local res= inner "$1" echo "res: ${res[@]}" } The "unset" in inner() actually unsets the "res" in outer() and the following assignment (to res[0] and res[1]) is actually assigning to the global var "res". And in outer(), after calling inner(), its local "res" is gone and it also referencing to the global var "res". With 2 "unset" commands, even the global "res" is unset.