trap-expands-now
What it does
Section titled “What it does”Reports a trap whose command is double-quoted and contains an expansion, as in trap "rm $tmp" EXIT.
Why it matters
Section titled “Why it matters”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.
Example
Section titled “Example”...printf '%s' "$x" "a $b"printf 'literal $x'trap "rm $tmp" EXITtrap 'rm $tmp' EXITtrap "echo bye" EXIT...Configuration
Section titled “Configuration”shell: lint: rules: trap-expands-now: warn # off, warn (default), errorEnabled by the presets: shell/shellcheck.
Suppress one line with sudsy-disable-next-line trap-expands-now in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools; Sudsy reports what each upstream rule reports.
| Tool | Rule | Match |
|---|---|---|
| shellcheck | SC2064 |
full |