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: John Kearney
Subject: Re: [Help-bash] inconsistency in expansions
Date: Mon, 07 May 2012 12:15:16 +0200
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1

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
${${#}}

Anyway as a work around these work.
${@:${#}:1}
or
${@:(-1):1}


or for your example
echo "${*%${@:(-1):1}}"
echo "${*%${@:${#}:1}}"


HTH


--
View
          John Kearney's profile on LinkedIn John
          Kearney

reply via email to

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