Skip to content

Files

Latest commit

 

History

History
37 lines (23 loc) · 1.02 KB

SC2155.md

File metadata and controls

37 lines (23 loc) · 1.02 KB

Pattern: Masking return value in shell script

Issue: -

Description

In the original code, the return value of somecmd 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 somecmd. This avoids the problem.

Example of incorrect code:

export foo="$(somecmd)"

Example of correct code:

foo=$(somecmd)
export foo

Exceptions

If you intend to ignore the return value of an assignment, you can either ignore this warning or use

foo=$(somecmd) || true
export foo

Shellcheck does not warn about export foo=bar because bar is a literal and not a command substitution with an independent return value. It also does not warn about local -r foo=$(cmd), where declaration and assignment must be in the same command.

Further Reading