On 23/03/2018 10:52, Herbert Xu wrote:
On Fri, Mar 23, 2018 at 10:27:20AM +0100, Harald van Dijk wrote:
On 23/03/2018 10:10, Herbert Xu wrote:
On Fri, Mar 23, 2018 at 09:27:37AM +0100, Harald van Dijk wrote:

Also the above. But it breaks a traditional ash extension:

   unset IFS
   set -- A1 B2 C3
   echo ${@%2 C3}

This used to print A1 B, but prints A1 B2 C3 with your patch.

   echo ${@%2}

This used to print A1 B2 C3, but prints A1 B with your patch.

Hmm, it still does on my machine:

Apologies, I ended up sending the wrong test case. It's when IFS has its
default value that the behaviour is changed. It's when it's unset that it
still works.

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.

The second example looks strange, but bash/ksh does something funky
too in this case:

bash-4.2$ echo ${*%2}
A1 B C3
bash-4.2$
>
> We could change dash to match this behaviour as it seems to make
> a little bit more sense but I'm not too bothered.

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

Cheers,
Harald van Dijk
--
To unsubscribe from this list: send the line "unsubscribe dash" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to