Glob Pattern Safety Medium

Using bare glob patterns can cause files with dash-prefixed names to be mistakenly interpreted as command options. To fix this, prefix glob patterns with './' or use '--' before them to ensure all matching files, including those with dash-prefixed names, are treated as arguments rather than options.

Detector ID
shell/glob-pattern-safety@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-
Tags
-

Noncompliant example

1
2# Noncompliant: Can accidentally delete files with names starting with '-'.
3rm *

Compliant example

1
2# Compliant: Safely removes all files in current directory, even those starting with '-'.
3rm ./* 2>/dev/null || rm -- *