Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary use of $ / ${} with arithmetic variable - Critical anti-pattern #143

Open
philipjonsen opened this issue Sep 22, 2023 · 0 comments

Comments

@philipjonsen
Copy link

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]))

You can find $/${} is unnecessary on arithmetic variables here:

/mainnet/blob/main/shentu-2.2/statesync/state_sync.sh#L13-L13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant