Debian Bug report logs - #774001
${#@} and ${#*} are incompatable with bash and posh

version graph

Package: dash; Maintainer for dash is Andrej Shadura <[email protected]>; Source for dash is src:dash (PTS, buildd, popcon).

Reported by: Joey Hess <[email protected]>

Date: Fri, 26 Dec 2014 23:33:06 UTC

Severity: normal

Found in version dash/0.5.7-4

Full log


Message #5 received at [email protected] (full text, mbox, reply):

Received: (at submit) by bugs.debian.org; 26 Dec 2014 23:31:07 +0000
From [email protected] Fri Dec 26 23:31:07 2014
X-Spam-Checker-Version: SpamAssassin 3.3.2-bugs.debian.org_2005_01_02
	(2011-06-06) on buxtehude.debian.org
X-Spam-Level: 
X-Spam-Status: No, score=-10.9 required=4.0 tests=BAYES_00,FOURLA,HAS_PACKAGE,
	PGPSIGNATURE,SPF_HELO_PASS,SPF_PASS,STOCKLIKE,SUBJMONEY,T_RP_MATCHES_RCVD
	autolearn=ham version=3.3.2-bugs.debian.org_2005_01_02
X-Spam-Bayes: score:0.0000 Tokens: new, 5; hammy, 151; neutral, 102; spammy,
	0. spammytokens: hammytokens:0.000-+--H*u:2014-03-12,
	0.000-+--H*UA:2014-03-12, 0.000-+--H*u:1.5.23, 0.000-+--H*UA:1.5.23,
	0.000-+--systemd
Return-path: <[email protected]>
Received: from kite.kitenet.net ([66.228.36.95] helo=kitenet.net)
	by buxtehude.debian.org with esmtp (Exim 4.80)
	(envelope-from <[email protected]>)
	id 1Y4eLa-00019A-RA
	for [email protected]; Fri, 26 Dec 2014 23:31:07 +0000
Received: by kitenet.net
Date: Fri, 26 Dec 2014 19:30:52 -0400
From: Joey Hess <[email protected]>
To: Debian Bug Tracking System <[email protected]>
Subject: ${#@} and ${#*} are incompatable with bash and posh
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha1;
	protocol="application/pgp-signature"; boundary="HcAYCG3uE/tztfnV"
Content-Disposition: inline
X-Reportbug-Version: 6.6.1
User-Agent: Mutt/1.5.23 (2014-03-12)
Delivered-To: [email protected]
[Message part 1 (text/plain, inline)]
Package: dash
Version: 0.5.7-4+b1
Severity: normal

joey@darkstar:~>cat testcase 
#!/bin/sh
echo ${#@}

joey@darkstar:~>bash testcase a
1
joey@darkstar:~>posh testcase a
1
joey@darkstar:~>dash testcase a
1

good so far...

joey@darkstar:~>bash testcase a b
2
joey@darkstar:~>posh testcase a b
2
joey@darkstar:~>dash testcase a b
3

!! 

joey@darkstar:~>bash testcase a b c
3
joey@darkstar:~>posh testcase a b c
3
joey@darkstar:~>dash testcase a b c
5

joey@darkstar:~>bash testcase aaa bbb cccc
3
joey@darkstar:~>posh testcase aaa bbb cccc
3
joey@darkstar:~>dash testcase aaa bbb cccc
12


posh:
       ${#name}
           The number of positional parameters if name is *, @ or is
           not specified, or the length of the string value of
           parameter name.

bash:

	${#name[subscript]}  expands  to  the  length  of  ${name[sub‐
       script]}.  If subscript is * or @, the expansion is the number
       of elements in the array.

dash:

     ${#parameter}         String Length.  The length in characters
                           of the value of parameter.

So dash's documentation doesn't specify what it does for ${#@}. What its
actually doing is taking the length of the string consisting of all the
positional parameters, separated by spaces.

I have not checked if the behavior of ${#@} is standardized in any way.
If ${#@} is standardized, then dash should follow the standard.

Currently, a #!/bin/sh script cannot currently safely use ${#@}, because
/bin/sh can be dash or bash. If some package contains a #!/bin/dash
script, it could rely on the current behavior. OTOH, the behavior is
undocumented, and seems pretty useless, so it's unlikely someone would
go to the bother of using #!/bin/dash to get it. This might argue for
changing the behavior to match bash and posh even if there is not a
standard.

On the other hand, $# reliably yields the number of positional parameters
in all of bash, posh, and dash. So any script can use that. So, the easiest
fix would be to just document on its man page the special way bash handles
${#@} (and ${#*})

-- System Information:
Debian Release: 8.0
  APT prefers unstable
  APT policy: (500, 'unstable'), (1, 'experimental')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 3.16.0-4-amd64 (SMP w/4 CPU cores)
Locale: LANG=en_US.utf8, LC_CTYPE=en_US.utf8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)

Versions of packages dash depends on:
ii  debianutils  4.4+b1
ii  dpkg         1.17.22
ii  libc6        2.19-13

dash recommends no packages.

dash suggests no packages.

-- debconf information excluded

-- 
see shy jo
[signature.asc (application/pgp-signature, inline)]

Send a report that this bug log contains spam.


Debian bug tracking system administrator <[email protected]>. Last modified: Tue May 13 14:17:22 2025; Machine Name: buxtehude

Debian Bug tracking system

Debbugs is free software and licensed under the terms of the GNU General Public License version 2. The current version can be obtained from https://bugs.debian.org/debbugs-source/.

Copyright © 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson, 2005-2017 Don Armstrong, and many other contributors.