Use double dash before user input
Put -- before a variable that holds a file name, so a value such as -rf or -v is read as an operand and not as an option. It closes option injection.
A script passes a name from a user or a directory listing to ls or rm, and a file called -A turns into an option.
touch ./-A ./normal.txt
f=-A
ls "$f"
echo ---
ls -- "$f"
rm -- "$f"
echo ---
ls-A
main.sh
normal.txt
---
-A
---
main.sh
normal.txtQuoting stops word splitting but not option parsing, so ls "-A" is still the -A option and lists every entry instead of the one file. The -- argument marks the end of options in the POSIX utility conventions, and everything after it is an operand. The second listing shows only the file, and rm -- then removes it, which a bare rm -A could not do since it is not an option of rm.
Not every program honors --, and some, like echo, print it. For those, prefix relative paths with ./ so the name cannot begin with a dash. A glob like * is safe in a directory listing only when the expansion starts with ./*.