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

Re: [PATCH 0/3] Fix $((...)) coding style

From
Junio C Hamano <junio@pobox.com>
Date
Feb 4, 2016, 19:43 UTC
Message-ID
<xmqqtwlogt1d.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20160204155316.GI29880@serenity.lan>
Junio C Hamano <gitster@pobox.com> writes:
Show 16 quoted lines
> One thing I was wondering about the $(( ... )) syntax while reading
> this thread was about the SP around the expression, i.e.
>
> 	var=$(( $term1 * $term2 + $term3 ))
>
> vs
>
> 	var=$(($term1 * $term2 + $term3))
>
> I personally do not have strong preference between the two, but I
> have a vague impression that we preferred the former because
> somebody in the past gave us a good explanation why we should.
>
> "git grep" however seems to tell us that we are not clearly decided
> between the two, so I probably am misremembering it and there is no
> preference either way.

One thing that is somewhat related is that we would want to avoid writing things like this by mistake:

	var=$((cmd1 && cmd2) | cmd3)

which is not meant to be an arithmetic expansion, but is a command substitution that happens to have a subshell at the head of the pipeline. I think bash gets this right, but some shells (e.g. dash) tries to parse this as an arithmetic expansion upon seeing "$((" and fails to parse it, waiting forever until it sees the matching "))". So we write it like this to make it safer:

	var=$( (cmd1 && cmd2) | cmd3 )
Perhaps having a SP after $(( of a real arithmetic expression, i.e.
	var=$(( ... anything ... ))
makes it easier to tell these two apart?  I dunno.
Previous: Junio C Hamano
Message 13 of 13 in “Fix $((...)) coding style”
  1. 0/3 Fix $((...)) coding styleJohannes Schindelin, Feb 4, 2016
  2. 1/3 filter-branch: fix style of $((...) constructJohannes Schindelin, Feb 4, 2016
  3. 2/3 rebase--interactive: adjust the coding style of $((...))Johannes Schindelin, Feb 4, 2016
  4. 3/3 rebase --merge: adjust $((...)) coding styleJohannes Schindelin, Feb 4, 2016
  5. John KeepingFeb 4, 2016
  6. Johannes SchindelinFeb 4, 2016
  7. John KeepingFeb 4, 2016
  8. Johannes SchindelinFeb 4, 2016
  9. John KeepingFeb 4, 2016
  10. Johannes SchindelinFeb 4, 2016
  11. John KeepingFeb 4, 2016
  12. Junio C HamanoFeb 4, 2016
  13. Junio C HamanoFeb 4, 2016

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.