bug-bash
[Top][All Lists]
Advanced

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

Re: Expansion of ${!x*} and ${!x@}


From: Chet Ramey
Subject: Re: Expansion of ${!x*} and ${!x@}
Date: Wed, 8 Nov 2006 11:08:36 -0500

>      ${!prefix*}
>      ${!prefix@}
>         Expands to the names of variables whose names begin with prefix,
>         separated by the first character of the IFS special variable.
> 
> ---
>  From this, I'd expect both forms to have the same output.
> Is this what is intended and what I should expect?
> 
> For test purposes, I set:
>    IFS="<" OFS=">"   # (also set "Output" FS to see if it is used
>                      #  in the output of 'echo')
> ---
> I used 4 tests, all with prefix=U, 1st pair unquoted, 2nd pair quoted.
> input:
>    echo ' * ' =  ${!U*}    ; \     # (line 1 - * unquoted)
>    echo ' @ ' =  ${!U@}    ; \     # (line 2 - @ unquoted)
>    echo '"*"' = "${!U*}"   ; \     # (line 3 - * quoted)
>    echo '"@"' = "${!U@}"           # (line 4 - @ quoted)
> ---
> output:
>     *  = UID USER                  # (line 1)
>     @  = UID USER                  # (line 2)
>    "*" = UID<USER                  # (line 3)
>    "@" = UID USER                  # (line 4)
> ---
> 
> QUESTIONS continued...
> - If the two forms are supposed to be identical, why aren't lines
>    3 & 4 the same?

I think adding this sentence to the man page will explain it:

When @ is used and the expansion appears within double quotes, each
variable name expands to a separate word.

Similar language appears in the description of other expansions that
do the same sort of thing (e.g., ${!name[@]}). 

> - Why do the quotes in line 3 make for different output than in line
> Why aren't the 4 lines identical?  I see the IFS, "<" in line 3, but
> not in lines 1,2&4.  Why isn't it in all 4 lines and, also, even
> the double quotes make a difference when expanding variables, why
> aren't lines 3&4 the same?

It's not in the first two lines because it's removed as part of word
splitting.  When word splitting is inhibited by double quotes, the
above sentence from the man page will explain the difference in output.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                                Live Strong.
Chet Ramey, ITS, CWRU    chet@case.edu    http://tiswww.tis.case.edu/~chet/




reply via email to

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