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: Mon, 07 May 2012 06:35:19 -0500
User-agent: KMail/4.8.3 (Linux/3.3.4-pf+; KDE/4.8.3; x86_64; ; )

On Monday, May 07, 2012 12:15:16 PM John Kearney wrote:
> Am 07.05.2012 02:07, schrieb Dan Douglas:
> > 
> > ${$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

You're mistaken about the form of a parameter expansion. A parameter expansion 
in Bash is either `$name', or consists of: `${' followed by potentially either 
a `!' or `#' followed by a "parameter name", followed by an optional 
"operator", followed by a context whose evaluation depends upon the preceeding 
operator, followed by a closing brace.

I can't guess how you were expecting ${${#}} to be interpreted. Perhaps You 
meant ${!#} ? (which is sort of a hack I wouldn't recommend for clarity.) The 
"parameter name" I mentioned above MUST be of the form `var' or `arr[expr or @ 
or *]`. No "nesting" of expansions can ever take place before the "operator" 
except within an array index specification. If the prefix is !, then the value 
of the given parameter must also be of the aforementioned form. Typically, 
expansions may be nested to the right of the operator.

Thus

 ${x#${y%z}}

is a valid parameter expansion.

Some fun with the '+' substitution:

 $ rev() { ${2+rev "${@:2}"}; printf '%s ' "$1"; }; rev {1..5}
5 4 3 2 1

-- 
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]