zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-workers@zsh.org
Subject: [austin-group] Re: clarification on $*/$@
Date: Wed, 22 Oct 2014 13:52:02 -0700 (PDT)	[thread overview]
Message-ID: <alpine.LRH.2.00.1410221324400.26348@toltec.zanshin.com> (raw)
In-Reply-To: <54480AFD.9010504@case.edu>

Extensive quoting for context.

On Wed, 22 Oct 2014, Chet Ramey wrote:

> On 10/20/14, 5:54 PM, Stephane Chazelas wrote:
>
> > In the Bourne shell, $@ and $* were the same and were variables
> > whose content was the concatenation of the positional
> > parameters.
> >
> > The only thing special was "$@" ($@ quoted) in list contexts
> > (like in arguments to simple commands or in for i in "$@").
> >
> > So in:
> >
> > set a b@c
> > IFS=:,@
> > printf '<%s>\n' $*
> >
> > $* expands to "a:b@c" and undergoes word splitting.
>
> Not really, if you meant how the historical Bourne shell behaved.  The
> Bourne shell expands it to "a b@c" and performs word  splitting, resulting
> in
>
> <a b><c>

Hmm, zsh in sh-emulation behaves as Stephane describes:

$ set a b@c
$ IFS=:,@
$ printf '<%s>\n' $*
<a>
<b>
<c>
$ printf '<%s>\n' "$*"
<a:b@c>

> > set a b@c
> > IFS=
> > printf '<%s>\n' $@
> >
> > $@ expands to one argument only: "a b@c"
>
> Yes, this is what the Bourne shell did/does.

Hmm again, zsh:

$ IFS=
$ printf '<%s>\n' $@
<a>
<b@c>
$ unset IFS
$ printf '<%s>\n' $@
<a>
<b@c>

> > ksh introduced the joining on the joining on the first character
> > of IFS instead of a space character and arrays. $* and $@ still
> > seem to contain the concatenation of the positional parameters
> > with the first character of IFS except that in list contexts,
> > unquoted $* and $@ are also split on the positional parameters
> >
> > set a b
> > IFS=
> > a=$@
> >
> > assigns "ab" to $a, but
>
> That's not true: sh, ksh, bash, and so on assign "a b".  If you meant to
> use $*, you're correct.  Only $* is specified to use the first character
> of $IFS.

Zsh joins all arrays on the first character of IFS in this context.  Note
that assignment is one of the few contexts in which an array is joined
into a scalar.  [[ ]] is another.

$ set a b
$ IFS=
$ a=$@
$ print $a
ab
$ b=(b a)
$ a=${b[@]}
$ print $a
ba
$ [[ $@ = ab ]] && echo joined on IFS
joined on IFS

> If this is central to your argument, you might want to reframe it.

Is this an incompatibility that we should consider fixing?

> > set a b
> > IFS=
> > printf '<%s>\n' $@
> >
> > expands to 2 arguments "a" and "b".
>
> Yes, ksh/mksh and bash do this.  The historical Bourne shell expands it to
> one argument: "a b".

Zsh is as ksh here:

$ set a b
$ IFS=
$ printf '<%s>\n' $@
<a>
<b>

> > So:
> >
> > IFS=
> > set a b
> > printf '<%s>\n' $@
> >
> > would expand to one "ab" argument as you'd expect (as a logical
> > continuation of the Bourne shell and to be consistent with
> > a=$@ and from a shell that doesn't support arrays).
>
> One wouldn't.  Only dash does this.

Command-line arguments are not a context in which arrays are joined in
zsh, either.  Dash surprises me.

> > bash behaviour is rather odd. In some places $@ and $* seem to
> > be the concatenation of arguments with the first character of
> > IFS, sometimes with space.
> >
> > $ bash -c 'set a b; IFS=; a="$@"; echo "$a"'
> > a b
> > $ bash -c 'set a b; IFS=; a=$*; echo "$a"'
> > ab
> > $ bash -c 'set a b; IFS=; a="a bc"; echo ${a#$*}'
> > c
>
> Yes, the third case is a problem.  The other two cases are consistent with
> ksh93, mksh, and other Posix shells except dash.

Zsh gives:

% ARGV0=sh zsh -fc 'set a b; IFS=; a="$@"; echo "$a"'
ab
% ARGV0=sh zsh -fc 'set a b; IFS=; a=$*; echo "$a"'
ab
% ARGV0=sh zsh -fc 'set a b; IFS=; a="a bc"; echo ${a#$*}'
a bc


       reply	other threads:[~2014-10-22 21:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20141020215420.GB7819@chaz.gmail.com>
     [not found] ` <54480AFD.9010504@case.edu>
2014-10-22 20:52   ` Bart Schaefer [this message]
2014-10-23  9:29     ` Stephane Chazelas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LRH.2.00.1410221324400.26348@toltec.zanshin.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).