[Top][All Lists]

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

Indices of array variables are sometimes considered unset (or just displ

From: Great Big Dot
Subject: Indices of array variables are sometimes considered unset (or just display an error).
Date: Tue, 6 Nov 2018 17:29:51 -0500

On 11/5/18 8:44 PM, Great Big Dot wrote:

> > What's actually happening here is that the *indirection* expansion
> > "${!foo}", and not the *indices* expansion "address@hidden", is what is 
> > being
> > preformed on something like "address@hidden". Both expansions, while
> > unrelated, happen to use the same syntax, with the exception that
> > indirections apply to normal variables and index expansions apply to array
> > variables. For some reason, adding on the "${foo-default}" expansion causes
> > the former to be used instead of the latter.

> The `some reason' is that the current behavior is documented and has been
> the way bash has worked since I added indirect expansion (bash-2.0) and the
> array keys expansion (bash-3.0).

Fair enough. In retrospect, the dual usage of "!" should have been the
first thing I should have blamed when it didn't behave as I expected.

> > $ foo='hello world'
> > $ printf -- '%s\n' "address@hidden"
> > hello world

> (I don't get that result with any version of bash. I went back to bash-3.0
> before I quit trying.)

Oops, must have made a copy-paste error. Sorry for wasting your time
with that! What I meant was something like:

    $ foo='hello world'
    $ array=(foo)
    $ echo "address@hidden"
    hello world

>> Upon further examination, and in light of the above realization, this
>> actually isn't true. In particular, iff the first character of IFS is
>> alphanumeric or an underscore (or if IFS is the empty string), and if you
>> use the "${array[*]}" form instead, then the expansion doesn't throw an
>> error when the array contains more than one element.

> Sure, since a double-quoted expansion using `*' separates words using the 
> first character of IFS.

Yeah, that was what led me to make that observation. Before it hit me
that bash was interpreting it as an indirection, on a hunch I messed
around a bit with IFS and nothing seemed to change, and said so. After
I realized what was happening, I just wanted to correct my false claim
that IFS was irrelevant.

>> Is there a good reason for treating "address@hidden" and "${!array[*]-}"
>> like indirections instead of index expansions (or just throwing an error)?

> When I added array variables and the array keys expansion, I used the ksh93
> syntax (${!var[sub]}) and tried to avoid conflict with the existing
> indirect expansion as much as possible (that was back when we still thought
> there was a chance that POSIX would standardize arrays and it was useful to
> have consistent implementations). The ksh93 expansion syntax made it
> invalid to use the array keys expansion as part of the ${param:-word}
> expansion, so that's how I made it work. Since it wasn't a candidate for
> that family of expansions, the existing variable indirection syntax
> controls.

Makes sense. IMO, it still might be worth mentioning explicitly in the
manual what happens when you try to mix and match various expansions,
at least in cases where there's more than one reasonable
interpretation. (Granted, a closer reading of the relevant sections
would have made it clear that *only* address@hidden and array[*] were privy
to the secondary meaning of "!", as opposed to variations.)

reply via email to

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