|
From: | John Kearney |
Subject: | Re: [Help-bash] inconsistency in expansions |
Date: | Mon, 07 May 2012 15:04:12 +0200 |
User-agent: | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 |
Am 07.05.2012 13:35, schrieb Dan Douglas:
Firstly I never said i didn't understand why ${$#} didn't work that was clear enough.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 DouglasActually 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}}" HTHYou'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 The how I got there is address@hidden equiv to address@hidden or $@ ${array[1]} equiv to $1 or ${1} ${array[10]} equiv to ${10} so .... ${array[-1]} not equiv to ${-1} address@hidden not equiv to ${${#}} I can understand why this is tricky for the parser. I think i keep forgetting that this doesn't work. but it does seem a kinda natural step. $1 to ${1} to ${$#} or even ${-1} because ${value:(-1)} works, I even gave ${($#})} a go out of curiosity. the confusing part is that $1 ...$9 are handled special. a lot of noob also think this should work address@hidden so instead we use the split functionality and do ${@:$#:1} Just trying to point out why it confuses me every now and again. |
[Prev in Thread] | Current Thread | [Next in Thread] |