bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Unset array doesn't work


From: Nikolai Kondrashov
Subject: Re: Unset array doesn't work
Date: Mon, 12 Feb 2018 10:05:47 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0

On 02/12/2018 10:03 AM, Clark Wang wrote:
On Mon, Feb 12, 2018 at 3:23 PM, Nikolai Kondrashov <spbnick@gmail.com 
<mailto:spbnick@gmail.com>> wrote:


        Take a look at these links:

           - http://wiki.bash-hackers.org/commands/builtin/unset 
<http://wiki.bash-hackers.org/commands/builtin/unset>
           - http://www.fvue.nl/wiki/Bash:_Passing_variables_by_reference 
<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.

Thank you, that explains it wonderfully. Sorry for being lazy and not figuring
it out myself. Now I need to craft a fix :)

Nick



reply via email to

[Prev in Thread] Current Thread [Next in Thread]