Re: ${1+"$@"} does not generate multiple words if IFS is empty

2015-12-30 Thread Andreas Schwab
mart...@inlv.org writes:

>   The substitution ${1+"$@"} should resolve to "$@" if there is at
>   least one parameter -- i.e. one word per parameter. This works fine
>   if IFS contains any character or is unset. If IFS is empty, it
>   instead resolves to the equivalent of "$*", i.e. a single word
>   concatenating all the parameters without a separator. IFS should
>   not influence the behaviour of "$@" under any circumstances.

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05

"If the value of IFS is null, no field splitting shall be performed."

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."



Re: ${1+"$@"} does not generate multiple words if IFS is empty

2015-12-30 Thread Greg Wooledge
On Wed, Dec 30, 2015 at 10:02:41AM +0100, Andreas Schwab wrote:
> mart...@inlv.org writes:
> 
> > The substitution ${1+"$@"} should resolve to "$@" if there is at
> > least one parameter -- i.e. one word per parameter. This works fine
> > if IFS contains any character or is unset. If IFS is empty, it
> > instead resolves to the equivalent of "$*", i.e. a single word
> > concatenating all the parameters without a separator. IFS should
> > not influence the behaviour of "$@" under any circumstances.
> 
> http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05
> 
> "If the value of IFS is null, no field splitting shall be performed."

By itself that doesn't explain it.  But if you scroll up to
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_02
it says:

  @
  Expands to the positional parameters, starting from one. When the
  expansion occurs within double-quotes, and where field splitting (see
  Field Splitting) is performed, each positional parameter shall expand
  as a separate field [...]



Re: ${1+"$@"} does not generate multiple words if IFS is empty

2015-12-30 Thread Chet Ramey
On 12/29/15 10:40 PM, mart...@inlv.org wrote:
> Bash Version: 4.3
> Patch Level: 42
> Release Status: release
> 
> Description:
>   The substitution ${1+"$@"} should resolve to "$@" if there is at
>   least one parameter -- i.e. one word per parameter. This works fine
>   if IFS contains any character or is unset. If IFS is empty, it
>   instead resolves to the equivalent of "$*", i.e. a single word
>   concatenating all the parameters without a separator. IFS should
>   not influence the behaviour of "$@" under any circumstances.

Yes, thanks for the report.  This was `re-clarified' in a Posix group
discussion in October 2014 concerning how "$@" should be expanded in
various contexts, including those where field splitting would not be
performed.  It will be fixed for the next bash release.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



${1+"$@"} does not generate multiple words if IFS is empty

2015-12-30 Thread martijn
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: darwin11.4.2
Compiler: /usr/bin/clang
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' 
-DCONF_OSTYPE='darwin11.4.2' -DCONF_MACHTYPE='x86_64-apple-darwin11.4.2' 
-DCONF_VENDOR='apple' -DLOCALEDIR='/opt/local/share/locale' -DPACKAGE='bash' 
-DSHELL -DHAVE_CONFIG_H -DMACOSX   -I.  -I. -I./include -I./lib  
-I/opt/local/include -pipe -Os -DSSH_SOURCE_BASHRC -arch x86_64
uname output: Darwin breedzicht.local 11.4.2 Darwin Kernel Version 11.4.2: Thu 
Aug 23 16:25:48 PDT 2012; root:xnu-1699.32.7~1/RELEASE_X86_64 x86_64
Machine Type: x86_64-apple-darwin11.4.2

Bash Version: 4.3
Patch Level: 42
Release Status: release

Description:
The substitution ${1+"$@"} should resolve to "$@" if there is at
least one parameter -- i.e. one word per parameter. This works fine
if IFS contains any character or is unset. If IFS is empty, it
instead resolves to the equivalent of "$*", i.e. a single word
concatenating all the parameters without a separator. IFS should
not influence the behaviour of "$@" under any circumstances.

(d)ash variants, AT&T ksh, mksh, zsh, yash all do this correctly.
bash 2.05b.13 also does this correctly (!). The bug is present in
bash 3.2.57 and bash 4.3.42.

(In bash, ${1+"$@"} is redundant, but in some old versions of ksh
and other shells, if 'set -u' is active, "$@" throws an error if
there are no parameters, so ${1+"$@"} is used as a workaround. As a
result, this is often found in cross-platform scripts. These break
on bash 3 & 4 if fieldsplitting is disabled by emptying IFS.)

Repeat-By:
$ IFS=''# bug only occurs on empty IFS
$ set -- this is a test
$ printf '|%s|\n' ${1+"$@"} # generates wrong output
|thisisatest|
$ printf '|%s|\n' "$@"  # generates correct output
|this|
|is|
|a|
|test|