mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 11:19:45 +08:00
Created SC2155 (markdown)
23
SC2155.md
Normal file
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
|
Reference in New Issue
Block a user