bug-bash
[Top][All Lists]
Advanced

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

Re: Indices of array variables are sometimes considered unset (or just d


From: Chet Ramey
Subject: Re: Indices of array variables are sometimes considered unset (or just display an error).
Date: Tue, 6 Nov 2018 09:44:38 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.9.1

On 11/5/18 4:42 PM, Great Big Dot wrote:

> Description:
>       The parameter expansion "${!var[@]}" expands to the indices of an array
>     (whether linear or associative). The expansion "${var-string}"
>     returns "${var}" iff var is set and 'string' otherwise. These two
>     features do not play well together:

You seem to have neglected a significant section of the documentation:

"If  the  first  character of parameter is an exclamation point (!), and
parameter is not a nameref, it introduces a level of indirection.  Bash
uses  the  value  formed  by expanding the rest of parameter as the new
parameter; this is then expanded and that value is used in the rest  of
the  expansion,  rather  than  the expansion of the original parameter.
This is known as indirect expansion.  The value  is  subject  to  tilde
expansion,  parameter  expansion,  command substitution, and arithmetic
expansion.  If parameter is a nameref, this expands to the name of  the
parameter  referenced  by  parameter instead of performing the complete
indirect expansion.  The exceptions  to  this  are  the  expansions  of
${!prefix*}  and  ${!name[@]}  described  below.  The exclamation point
must immediately follow the left brace in order to  introduce  indirec-
tion."


>         $ declare -a -- array=([0]='helloworld')
>         $ printf -- '%s\n\n' "${!array[@]-Warning: unset}"
>         Warning: unset

This happens to "work" because there is a single array element set, and it
expands to a single word.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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