Pierre Gaston: > I think it's even more likely to happen with eg: read array[i]
Maybe, but see below. > There is a large number of pitfalls in bash > (http://mywiki.wooledge.org/BashPitfalls) that most people ignore. And it doesn't even mention the unset problem, I think. On Thu, Oct 26, 2017 at 8:56 AM, Chet Ramey <chet.ra...@case.edu> wrote: > On 10/26/17 1:18 AM, Eli Barzilay wrote: >> Bash surprised me with the behavior mentioned here: >> >> https://stackoverflow.com/questions/15897473 >> >> This can be pretty bad in that it's very unexpected (see the >> comments). > > I'm not sure why this is a surprise. Pathname expansion (globbing) is > one of the word expansions performed before a simple command is > executed. The `unset' builtin is no different. The last sentence is showing why it's a surprise: it is confusing since it easy to think that unset is special, similar to languages which have something like `delete foo[1]` where the thing that follows delete is an lvalue. >> The thing is that AFAICT, there is no mention of this pitfall in the >> man page... > > "The unset builtin is used to destroy arrays. unset name[subscript] > destroys the array element at index subscript. Negative subscripts to > indexed arrays are interpreted as described above. Care must be taken > to avoid unwanted side effects caused by pathname expansion." 1. This is much more indirect than a simple "always quote array references"; 2. I completely missed it since it's not in the place which describes unset. (BTW, when I did dare for the first time to use unset on an array I did go through the unset description, and got a vague impression that it's kind of doing the special lvalue thing, so possibly the indirect warning would have been sufficient to slap me back into the bash reality.) -- ((x=>x(x))(x=>x(x))) Eli Barzilay: http://barzilay.org/ Maze is Life!