ShellCheck rates SC2086 as info. It let rm delete two files I never named.

●1 ●30 ●94
calendar_today • schedule1 min read

SC2086 sits at info, ShellCheck's lowest default severity, and fires on every unquoted $var, so it is usually the first rule anyone disables. I built the smallest test I could: a directory holding quarterly, report.txt and quarterly report.txt, and a four-line script under set -euo pipefail:

report="quarterly report.txt"
rm $report

It deleted quarterly. It deleted report.txt. It left the one file it was asked to remove, and exited 0. rm was handed two names and both existed, so strict mode had nothing to catch.

Bash expands the unquoted variable, splits the result on whitespace, globs any * or ? against the current directory, and only then builds the argument list. rm "$report" is one argument with no globbing, and it deletes the right file.

The same bug hides in other shapes. [ $name = "root" ] with an empty $name gives test two arguments: a complaint on stderr, a false condition, and the script keeps walking. for f in $files is not flagged by ShellCheck 0.11.0 at all, and still breaks on the first name with a space.

When you do want the split, such as a flag string read from a config, use an array or a one-line # shellcheck disable=SC2086 with the reason. A repo-wide .shellcheckrc disable covers every future rm as well. And shellcheck --include=SC2086 runs it alone as a CI gate.

Every run, pasted from ShellCheck 0.11.0, is in the SC2086 deep dive on word splitting and globbing.

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

My 30-day log cleanup kept a file that was 30 and a half days old

BashSnippets - Oct 8

I deleted 44.6 GB and df moved by 5

BashSnippets - Nov 10

I killed the process I started. The port stayed taken.

BashSnippets - Nov 5

I escaped the dot correctly and sed still rewrote a file marked DO NOT CHANGE

BashSnippets - Oct 13

The log was deleted. Its 48M was still allocated.

BashSnippets - Oct 28
chevron_left
4k Points • 125 Badges
North America • bashsnippets.xyz
77Posts
35Comments
10Connections
Linux user who got tired of Googling the same bash commands every time I sat down at a terminal. Sta... Show more

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!