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: Pierre Gaston
Subject: Re: [Help-bash] inconsistency in expansions
Date: Mon, 7 May 2012 13:36:13 +0300



On Mon, May 7, 2012 at 1:15 PM, John Kearney <address@hidden> wrote:
Am 07.05.2012 02:07, schrieb Dan Douglas:

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.
Why this doesn't work 
${${#}}

-  ${$#}  is a special case of ${variable#} where "variable" is $
- $ is a special variable that expands to the pid of the shell
- ${variable#pattern} remove what is matched by pattern at the beginning or the variable content and expand to the result
- in your case pattern="" so it just doesn't do anything.
- so ${$#} is very different from what you seem to think it is and not at all related to ${${#}} which is just invalid syntax.

 



reply via email to

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