git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re*: [PATCH] t1503: Fix arithmetic expansion syntax error when using dash

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 22, 2010, 19:15 UTC
Message-ID
<7viq1xsi7a.fsf@alter.siamese.dyndns.org>
In-Reply-To
<4C98EF25.4070700@ramsay1.demon.co.uk>
Ramsay Jones <ramsay@ramsay1.demon.co.uk> writes:
Show 9 quoted lines
> On systems which have dash as /bin/sh, such as Ubuntu, the final
> test (master@{n} for various n) fails with a syntax error while
> processing an arithmetic expansion. The syntax error is caused by
> using a bare name ('N') as a variable reference in the expression.
>
> In order to avoid the syntax error, we spell the variable reference
> as '$N' rather than simply 'N'.
>
> Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Thanks.

POSIX wants shells to support both "N" and "$N" and requires them to yield the same answer to $((N)) and $(($N)), but we should aim for portability in a case like this, especially when the price we pay to do so is so small, i.e. a few extra dollars.

By the way, on my box, I get this:
    $ ls l /bin/dash
    -rwxr-xr-x 1 root root 104024 2008-08-26 02:36 /bin/dash*
    $ dpkg -l dash | grep '^ii'
    ii  dash              0.5.4-12          POSIX-compliant shell
    $ /bin/dash -c 'N=20 ; echo $(( N + 3 ))'
    23

I just left it vague by saying "e.g. older dash" in below, but we may want to be more precise in the documentation.

-- >8 --
CodingGuidelines: spell Arithmetic Expansion with $(($var))
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 Documentation/CodingGuidelines |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/Documentation/CodingGuidelines b/Documentation/CodingGuidelines
index b8bf618..2cdd76f 100644
--- a/Documentation/CodingGuidelines
+++ b/Documentation/CodingGuidelines
@@ -43,6 +43,10 @@ For shell scripts specifically (not exhaustive):
 
  - We use Arithmetic Expansion $(( ... )).
 
+ - Inside Arithmetic Expansion, spell shell variables with $ in front
+   of them, as some shells do not grok $((x)) while accepting $(($x))
+   just fine (e.g. older dash).
+
  - No "Substring Expansion" ${parameter:offset:length}.
 
  - No shell arrays.
Previous: Ramsay JonesNext: Jon Seymour
Message 2 of 7 in “t1503: Fix arithmetic expansion syntax error when using dash”
  1. t1503: Fix arithmetic expansion syntax error when using dashRamsay Jones, Sep 21, 2010
  2. Re*: [PATCH] t1503: Fix arithmetic expansion syntax error when using dashJunio C Hamano, Sep 22, 2010
  3. Jon SeymourSep 22, 2010
  4. Ramsay JonesSep 25, 2010
  5. Ævar Arnfjörð BjarmasonSep 25, 2010
  6. Ramsay JonesSep 29, 2010
  7. Junio C HamanoSep 25, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.