From b83ad9307144439e6f7b4dddca3e46edc97fc4ee Mon Sep 17 00:00:00 2001 From: koalaman Date: Sun, 8 Mar 2015 14:13:39 -0700 Subject: [PATCH] Created SC2155 (markdown) --- SC2155.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 SC2155.md 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