diff --git a/SC2155.md b/SC2155.md new file mode 100644 index 0000000..25e5e94 --- /dev/null +++ b/SC2155.md @@ -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