On Sunday, May 06, 2012 01:24:09 PM Bill
Gradwohl wrote:
>
> set -- "The cat " "ate the " "canary"
> echo $#
> x=$#
> declare string
> string="${*%${$#}}"
> echo $string
> string="${*%${$x}}"
> echo $string
> string="${*%${3}}"
> echo $string
>
> Produces:
>
> address@hidden ycc# ./tst
> 3
> The cat ate the canary
> ./tst: line 7: ${$x}: bad substitution
> The cat ate the canary
> The cat ate the
>
>
> I expected all of them to work.
>
> Why does bad substitution only occur
for $x and not for $#?
>
> $# version is ignored - doesn't work
and doesn't produce an error message.
>
> Aren't expansions done from the inside
out?
>
> --
> Bill Gradwohl
${$x} isn't a valid expansion, so it throws
an error, the pattern isn't matched, and nothing happens. ${$#}
expands to the shell's PID, and tries to chop off a pattern
matching the empty string at the beginning, which doesn't match
the pattern at the end of "$*", so nothing happens. "$3" expands
to "canary", which does match the pattern, so the string is
modified.
--
Dan Douglas
Actually I'm a bit confused now.