On Fri, Mar 23, 2018 at 12:19:28PM +0100, Harald van Dijk wrote: > > >Right. I think I'll leave this one alone. It worked purely by > >chance previously because dash incorrectly used the first IFS > >character even when it's outside of double-quotes, which is why > >the expansion ${@%2 C3} matches. > > When the first IFS character is a whitespace character, which it usually is, > it's not incorrect: inserting that character and then splitting on it > produces the exact results that POSIX requires. It's only when IFS doesn't > start with a whitespace character that it's incorrect. Where does POSIX require this? AFAICS the only time IFS comes into play in forming the fields is with "$*" which is not relevant here. @ Expands to the positional parameters, starting from one, initially producing one field for each positional parameter that is set. When the expansion occurs in a context where field splitting will be performed, any empty fields may be discarded and each of the non-empty fields shall be further split as described in Field Splitting. When the expansion occurs within double-quotes, the behavior is unspecified unless one of the following is true: Field splitting as described in Field Splitting would be performed if the expansion were not within double-quotes (regardless of whether field splitting would have any effect; for example, if IFS is null). The double-quotes are within the word of a ${parameter:-word} or a ${parameter:+word} expansion (with or without the <colon>; see Parameter Expansion) which would have been subject to field splitting if parameter had been expanded instead of word. If one of these conditions is true, the initial fields shall be retained as separate fields, except that if the parameter being expanded was embedded within a word, the first field shall be joined with the beginning part of the original word and the last field shall be joined with the end part of the original word. In all other contexts the results of the expansion are unspecified. If there are no positional parameters, the expansion of '@' shall generate zero fields, even when '@' is within double-quotes; however, if the expansion is embedded within a word which contains one or more other parts that expand to a quoted null string, these null string(s) shall still produce an empty field, except that if the other parts are all within the same double-quotes as the '@', it is unspecified whether the result is zero fields or one empty field. * Expands to the positional parameters, starting from one, initially producing one field for each positional parameter that is set. When the expansion occurs in a context where field splitting will be performed, any empty fields may be discarded and each of the non-empty fields shall be further split as described in Field Splitting. When the expansion occurs in a context where field splitting will not be performed, the initial fields shall be joined to form a single field with the value of each parameter separated by the first character of the IFS variable if IFS contains at least one character, or separated by a <space> if IFS is unset, or with no separation if IFS is set to a null string. > In bash and ksh nothing funky is going on. This is well-defined and > documented in the manuals: the trimming is done for each field. So when $# > is 3, ${@%pat} is equivalent to ${1%pat} ${2%pat} ${3%pat}, except that pat > is only expanded once. > > If you're interested in implementing that in dash, a relevant test case is > > set -- A1 B2 C3 > unset p > echo ${1%${p+x}${p=?}} ${2%${p+x}${p=?}} ${3%${p+x}${p=?}} > unset p > echo ${@%${p+x}${p=?}} > > In bash and ksh, this is supposed to print, and does print: > > A B2 C3 > A B C Fair enough. I'm not interested in implementing that right now. Cheers, -- Email: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt -- To unsubscribe from this list: send the line "unsubscribe dash" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html