Skip to content

trap-expands-now

Shellsuspiciousno fixdefault: warnshell/shellcheck

Reports a trap whose command is double-quoted and contains an expansion, as in trap "rm $tmp" EXIT.

The expansion happens when trap is set, not when the signal arrives. If the variable changes later, the trap uses the old value, and a value with a space or a quote changes the command.

To fix, use single quotes: trap 'rm "$tmp"' EXIT.

Reported
...
printf '%s' "$x" "a $b"
printf 'literal $x'
trap "rm $tmp" EXIT
trap 'rm $tmp' EXIT
trap "echo bye" EXIT
...
shell:
lint:
rules:
trap-expands-now: warn # off, warn (default), error

Enabled by the presets: shell/shellcheck.

Suppress one line with sudsy-disable-next-line trap-expands-now 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 SC2064 full