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.