Skip to content

declare-and-assign-separately

Shellsuspiciousno fixdefault: offshell/shellcheck

Reports export, local, declare, readonly or typeset with a command substitution as the value, as in local x=$(cmd).

The exit status of the line is that of local, which succeeds, and not of cmd. A failing command is lost, even under set -e.

To fix, declare first and assign on the next line: local x; x=$(cmd).

Reported
...
[[ $x = $y ]] && [[ -n $(foo) ]]
export out=$(make)
local out="$(make)" x=1
declare plain=1
for f in $files; do :; done
shell:
lint:
rules:
declare-and-assign-separately: warn # off (default), warn, error

Enabled by the presets: shell/shellcheck.

Suppress one line with sudsy-disable-next-line declare-and-assign-separately in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
shellcheck SC2155 full