Created SC2155 (markdown)

koalaman
2015-03-08 14:13:39 -07:00
parent 58908f61c2
commit b83ad93071

23
SC2155.md Normal file

@@ -0,0 +1,23 @@
## Declare and assign separately to avoid masking return values.
### Problematic code:
export foo="$(mycmd)"
### Correct code:
export foo
foo=$(mycmd)
### Rationale:
In the original code, the return value of `mycmd` is ignored, and `export` will instead always return true. This may prevent conditionals, `set -e` and traps from working correctly.
When first marked for export and assigned separately, the return value of the assignment will be that of `mycmd`. This avoids the problem.
### Exceptions:
If you intend to ignore the return value of an assignment, you can either ignore this warning or use
export foo
foo=$(mycmd) || true