smartcontractkit / chainlink

node of the decentralized oracle network, bridging on and off-chain computation
https://chain.link
Other
7.04k stars 1.72k forks source link

[DEVEL] Unnecessary use of $ / ${} with arithmetic variable SH-2004 critical anti-pattern #14512

Open philipjonsen opened 1 month ago

philipjonsen commented 1 month ago

Description

escription
Use of $ or ${..} on regular variables in arithmetic context is unnecessary, and can even lead to subtle bugs. This is because the content of $((..)) is first expanded into a string, and then evaluated as an expression:

$ a='1+1'
$ echo $(($a * 5))    # becomes 1+1*5
6
$ echo $((a * 5))     # evaluates as (1+1)*5
10
The $ is unavoidable for special variables like $1 vs 1, $# vs #. It is also required when adding modifiers to parameters expansions, like ${#var} or ${var%-}.

Problematic code:
echo $(($n + ${arr[i]}))
Preferred code:
echo $((n + arr[i]))

$/${} is unnecessary on arithmetic variables found here:

https://github.com/philipjonsen/chainlink/blob/develop/tools/benchmark/job_spec_delete_v2.sh#L143-L143

$/${} is unnecessary on arithmetic variables found here:

https://github.com/philipjonsen/chainlink/blob/develop/tools/benchmark/job_spec_delete_v2.sh#L134-L134