Skip to content

Files

Latest commit

 

History

History
57 lines (40 loc) · 755 Bytes

SC2084.md

File metadata and controls

57 lines (40 loc) · 755 Bytes

Pattern: Use of $((..))

Issue: -

Description

$((..)) expands to a number. If it's the only word on the line, the shell will try to execute this number as a command name:

$ i=4
$ $(( i++ ))
4: command not found
$ echo $i
5

To avoid trying to execute the number as a command name, use one of the methods mentioned:

$ i=4
$ _=$(( i++ ))
$ echo $i
5

Example of incorrect code:

i=4
$(( i++ ))

Example of correct code:

Bash, Ksh:

i=4
(( i++ ))

POSIX (assuming ++ is supported):

i=4
_=$(( i++ ))

Alternative POSIX version that does not preserve the exit code:

: $(( i++ ))

Further Reading