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 #8 received at [email protected] (full text, mbox, reply):

Received: (at 774001-submitter) by bugs.debian.org; 16 Aug 2015 13:40:09 +0000
From [email protected] Sun Aug 16 13:40:09 2015
X-Spam-Checker-Version: SpamAssassin 3.4.0-bugs.debian.org_2005_01_02
	(2014-02-07) on buxtehude.debian.org
X-Spam-Level: 
X-Spam-Status: No, score=-7.6 required=4.0 tests=BAYES_00,FOURLA,
	HAS_BUG_NUMBER,RP_MATCHES_RCVD,SPF_PASS autolearn=ham autolearn_force=no
	version=3.4.0-bugs.debian.org_2005_01_02
X-Spam-Bayes: score:0.0000 Tokens: new, 10; hammy, 150; neutral, 82; spammy,
	0. spammytokens: hammytokens:0.000-+--H*u:1.5.21, 0.000-+--H*UA:1.5.21,
	0.000-+--H*u:2010-09-15, 0.000-+--H*UA:2010-09-15, 0.000-+--testcase
Return-path: <[email protected]>
Received: from relay04.stack.nl
	([131.155.140.107] helo=mx1.stack.nl ident=postfix)
	by buxtehude.debian.org with esmtps (TLS1.0:DHE_RSA_CAMELLIA_256_CBC_SHA1:256)
	(Exim 4.84)
	(envelope-from <[email protected]>)
	id 1ZQyAT-0007bj-GT
	for [email protected]; Sun, 16 Aug 2015 13:40:09 +0000
Received: from snail.stack.nl (snail.stack.nl [IPv6:2001:610:1108:5010::131])
	by mx1.stack.nl (Postfix) with ESMTP id 1B2C3B8056
	for <[email protected]>; Sun, 16 Aug 2015 15:33:41 +0200 (CEST)
Received: by snail.stack.nl (Postfix, from userid 1677)
	id F257A28494; Sun, 16 Aug 2015 15:33:40 +0200 (CEST)
Date: Sun, 16 Aug 2015 15:33:40 +0200
From: Jilles Tjoelker <[email protected]>
To: [email protected]
Subject: Re: Bug#774001: ${#@} and ${#*} are incompatable with bash and posh
Message-ID: <[email protected]>
References: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
In-Reply-To: <[email protected]>
User-Agent: Mutt/1.5.21 (2010-09-15)
X-Greylist: delayed 383 seconds by postgrey-1.35 at buxtehude; Sun, 16 Aug 2015 13:40:09 UTC
On Fri, 26 Dec 2014 19:30:52 -0400 Joey Hess <[email protected]> wrote:
> Package: dash
> Version: 0.5.7-4+b1
> Severity: normal

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

> [snip]

> 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[subscript]}.  If subscript is * or @, the expansion is the
>        number of elements in the array.

Nitpick: this doesn't actually describe ${#@}. However, the actual
description of ${#parameter} further on explicitly documents what ${#@}
and ${#*} expand to, the same as $#.

> 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.

This is actually the length of the expansion of $@ or $* in a context
where word splitting is not performed, except if IFS is set to the empty
string.

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

POSIX leaves the result of the expansion of ${#@} and ${#*} unspecified.

> 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.

Dash's ${#*} may be a faster alternative to temp="$*"; ${#temp}.
However, the silently inconsistent behaviour across shells is a good
reason not to use it.

> 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 ${#*})

Indeed, there is no reason to use ${#@} and ${#*} in bash/posh.

-- 
Jilles Tjoelker



Send a report that this bug log contains spam.


Debian bug tracking system administrator <[email protected]>. Last modified: Tue May 13 14:18:06 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.