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


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

Re: null array[*] expansion not treated as null

Started byL A Walsh <bash@tlinx.org>
First post2018-11-25 12:02 -0800
Last post2018-11-25 12:02 -0800
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: null array[*] expansion not treated as null L A Walsh <bash@tlinx.org> - 2018-11-25 12:02 -0800

#14853 — Re: null array[*] expansion not treated as null

FromL A Walsh <bash@tlinx.org>
Date2018-11-25 12:02 -0800
SubjectRe: null array[*] expansion not treated as null
Message-ID<mailman.4610.1543176177.1284.bug-bash@gnu.org>
On 11/12/2018 1:32 PM, Grisha Levit wrote:
> When an array A has non-zero elements but the expansion of "${A[*]}" is
> still
> a null string, it is not treated as such for purposes of ${var:-X} expansion
> (though $* is treated as null in the same circumstance).
>
>     $ A=(''); set -- ''
>     $ echo "<${A[*]:-X}>" "<${*:-X}>"
>     <> <X>
>   
----
    Ah, a null string != empty.  Example:
>  A=()
>  declare -p A
declare -a A=()
>  A=('')                #null string
>  declare -p A
declare -a A=([0]="")    #a string is still a string, even if empty

Related:

FWIW, below, "IFS=" sets IFS to an empty string, like
IFS=''
>     $ IFS=
>     $ A=('' ''); set -- '' '';
>   
Typing in lines to echo the size of A and shell params:

>  echo "=$#"   ; echo number of parameters set by "set - '' ''"
=2
>  echo "size of A=${#A[@]}"
size of A=2

>     $ echo "<${A[*]:-X}>" "<${*:-X}>"
>     <> <X>
>   
---
    Going to use "«»" instead of "<>" cuz <> are shell chars that
need quoting (  otherwise: bash: syntax error near unexpected token `<'  ).

First, your echo:

>  echo "«${A[*]:-X}»" "«${*:-X}»"   
«» «X»            (#same)

and w/o the quotes:
>  echo «${A[*]:-X}» «${*:-X}»
« » « »

---
Notice adding quotes around your vars can change what is
echo'ed.  Looks like the output-separator ' ' (space) is
embedded in the '*'s of both expressions, but [*] undergoes
expansion, while the 2nd one doesn't.

So 1 hint to remember a variable is an internally quoted entity, such
if it is assigned to another var, it will be assigned "as-is", but
if you put double-quotes around a variable, it will replace sequences
of white space with 1 white space.

Does that address your examples' behaviors?

of some sort where '*'«»

[toc] | [standalone]


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


csiph-web