mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated Template (markdown)
36
Template.md
36
Template.md
@@ -1,17 +1,43 @@
|
|||||||
## (Message goes here)
|
## Remove '$' or use '_=$((expr))' to avoid executing output.
|
||||||
|
|
||||||
### Problematic code:
|
### Problematic code:
|
||||||
|
|
||||||
(Simple example of problematic code)
|
i=4
|
||||||
|
$(( i++ ))
|
||||||
|
|
||||||
### Correct code:
|
### Correct code:
|
||||||
|
|
||||||
(Simple example of above code, only fixed)
|
Bash, Ksh:
|
||||||
|
|
||||||
|
i=4
|
||||||
|
(( i++ ))
|
||||||
|
|
||||||
|
POSIX (assuming `++` is supported):
|
||||||
|
|
||||||
|
i=4
|
||||||
|
_=$(( i++ ))
|
||||||
|
|
||||||
|
Alternative POSIX version that does not preserve the exit code:
|
||||||
|
|
||||||
|
: $(( i++ ))
|
||||||
|
|
||||||
### Rationale:
|
### Rationale:
|
||||||
|
|
||||||
(An explanation of why the code is problematic and how the correct code is an improvement)
|
`$((..))` 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
|
||||||
|
|
||||||
### Exceptions:
|
### Exceptions:
|
||||||
|
|
||||||
(Cases where the user may choose to ignore this warning, if any.)
|
None.
|
Reference in New Issue
Block a user