help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] inconsistency in expansions


From: Dan Douglas
Subject: Re: [Help-bash] inconsistency in expansions
Date: Sun, 06 May 2012 19:07:16 -0500
User-agent: KMail/4.8.3 (Linux/3.3.4-pf+; KDE/4.8.3; x86_64; ; )

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

Attachment: signature.asc
Description: This is a digitally signed message part.


reply via email to

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