bug-bash
[Top][All Lists]
Advanced

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

Re: ="${@:...}" and ="${@/...}" join with IFS since after 4.4


From: Chet Ramey
Subject: Re: ="${@:...}" and ="${@/...}" join with IFS since after 4.4
Date: Tue, 18 Nov 2025 16:51:56 -0500
User-agent: Mozilla Thunderbird

On 11/18/25 1:46 AM, Robert Elz wrote:
     Date:        Mon, 17 Nov 2025 16:28:56 -0500
     From:        Chet Ramey <chet.ramey@case.edu>
     Message-ID:  <bb13177d-b759-4761-aff4-df5d73a1378a@case.edu>

   | This is easy enough to fix; the pattern expansion operators didn't know
   | the expansion was taking place on the rhs of an assignment statement.

I assume that "taking place on the rhs of an assignment" you really mean
"taking place in a situation where field splitting will not occur".

In general, yes. In this particular case, the inconsistency resulted from
a flag not getting passed down to the function that implements the pattern
removal operators. It's a niche case, so it's escaped notice so far.

Once that inconsistency gets fixed, we can talk about what the `right'
behavior should be.

I also hesitate to imagine how

        echo $(( $@ ))

is intended to be parsed (similarly $@ in a here doc, except that one
is probably to be treated as an operand of a redirect and so no field
splitting).

Well, everyone (including the NetBSD sh), manages to display `11' when
given these two scripts.

$ cat x3
set -- a + b
a=4
b=7

echo $(( $@ ))
$ cat x3a
set -- 7 + 4

echo $(( $@ ))

so I'm not going to worry too much about it.


ps: personally I think the sane behaviour (also easy to implement) is to
treat (always) $@ expansions in non-field-splitting contexts exactly as
if $* had been written (in all respects).   But as above, it is unspecified,
probably because, most likely for histerical reasons, many shells ignore
IFS and just use a space as a separator for $@ in that context.  Weird.

The behavior is pretty varied. Given these four commands:

set a b c; IFS=: o=${@-x}; printf '<%s>\n' "$o"
set a b c; IFS=: o="${@-x}"; printf '<%s>\n' "$o"
set a b c; IFS=: o=${@?x}; printf '<%s>\n' "$o"
set a b c; IFS=: o="${@?x}"; printf '<%s>\n' "$o"

There's the bash/ksh93/mksh/SVR4.2 sh camp, which uses spaces (except mksh
errors on the `?' expansion); the dash/yash/zsh camp, which consistenly
uses `:'; and the BSD sh camp, which uses spaces in the first command and
`:' in the rest.

Chet

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


reply via email to

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