Consider using `./` or `--` globSH-2035
It is recommended to use ./
or --
glob so names with dashes won't become options.
Problematic code:
rm *
Preferred code:
rm ./*
# or
rm -- *
Since files and arguments are strings passed the same way, programs can't properly determine which is which, and rely on dashes to determine what's what.
A file named -f
(touch -- -f
) will not be deleted by the problematic code. It will instead be interpreted as a command line option, and rm
will report a success.
Using ./*
will cause the glob to expanded into ./-f
, which won't be treated as an option.
Similarly, --
by convention indicates end of options, and nothing after it will be treated like flags (except for some programs possibly still special casing -
like stdin
).
Please note that changing *
to ./*
in GNU Tar parameters will add ./
prefix to path names in the created archive. This may cause subtle problems (eg. to search for a specific file in archive, the ./ prefix must be specified as well). So using -- *
is a safer fix for GNU Tar commands.
echo
and printf
do not have this issue unless the glob is the first word in the command.
For more information, see Filenames and Pathnames in Shell: How to do it Correctly.