Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14771 > unrolled thread

Re: Indices of array variables are sometimes considered unset (or just display an error).

Started byChet Ramey <chet.ramey@case.edu>
First post2018-11-06 09:44 -0500
Last post2018-11-06 09:44 -0500
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Indices of array variables are sometimes considered unset (or just display an error). Chet Ramey <chet.ramey@case.edu> - 2018-11-06 09:44 -0500

#14771 — Re: Indices of array variables are sometimes considered unset (or just display an error).

FromChet Ramey <chet.ramey@case.edu>
Date2018-11-06 09:44 -0500
SubjectRe: Indices of array variables are sometimes considered unset (or just display an error).
Message-ID<mailman.3568.1541515489.1284.bug-bash@gnu.org>
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/

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web