bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: More fun with IFS


From: Thorsten Glaser
Subject: Re: More fun with IFS
Date: Sun, 24 Feb 2013 22:26:52 +0000

Dan Douglas dixit:

>Zsh and pdkshes produce:
>
>one:::two:three:::four
>
>For all of the above, which I think is wrong for the last 4. ksh93 produces:

Why is it incorrect?

The mksh manpage documents $@ behaving like $*:

     @       Same as $*, unless it is used inside double quotes, in which case
             a separate word is generated for each positional parameter. If
             there are no positional parameters, no word is generated. $@ can
             be used to access arguments, verbatim, without losing NULL argu-
             ments or splitting arguments with spaces.

And $* uses the first char of IFS:

     *       All positional parameters (except 0), i.e. $1, $2, $3, ...
             If used outside of double quotes, parameters are separate words
             (which are subjected to word splitting); if used within double
             quotes, parameters are separated by the first character of the
             IFS parameter (or the empty string if IFS is NULL).

POSIX is just as explicit on $* (with better wording for the
two distinguished cases of IFS being unset or empty which the
mksh code implements correctly, though):

   *
          Expands  to  the positional parameters, starting from one.
          When  the  expansion  occurs within a double-quoted string
          (see  [54]Double-Quotes  ),  it  shall  expand to a single
          field  with  the  value of each parameter separated by the
          first  character  of  the IFS variable, or by a <space> if
          IFS  is unset. If IFS is set to a null string, this is not
          equivalent  to  unsetting it; its first character does not
          exist, so the parameter values are concatenated.

And POSIX on $@ doesn’t specify anything different for when the
result of $@ is used where it isn’t multiple fields:

   @
          Expands  to  the positional parameters, starting from one.
          When  the expansion occurs within double-quotes, and where
          field  splitting  (see [53]Field Splitting ) is performed,
          each  positional  parameter  shall  expand  as  a separate
          field,  with the provision that the expansion of the first
          parameter shall still be joined with the beginning part of
          the  original  word  (assuming that the expanded parameter
          was embedded within a word), and the expansion of the last
          parameter  shall still be joined with the last part of the
          original  word. If there are no positional parameters, the
          expansion of '@' shall generate zero fields, even when '@'
          is double-quoted.

So I think mksh at least behaves as specified, and the standard
doesn’t contradict it. Inside the code, there’s even special-casing
for “ifs0”, so I believe this is no accident.

In other words, “don’t do that then” (rely on this behaviour).
I think eval is evil anyway ;-)

(Thanks to ormaaj for pointing out this posting.)

bye,
//mirabilos
-- 
“It is inappropriate to require that a time represented as
 seconds since the Epoch precisely represent the number of
 seconds between the referenced time and the Epoch.”
        -- IEEE Std 1003.1b-1993 (POSIX) Section B.2.2.2




reply via email to

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