Disallow `rm` to remove `*` Note: If you googled this by the title of this question, don't use this script unless you know what it is supposed to do.

This is a script in bash 3+ that I have used for long for preventing rm * and rm -rf * from accidentally invoked and removing important files by mistake. I put it in my ~/.bash_aliases.

alias rm='set -f;rm'
rm(){
    if [[ "$-" == *i* ]]
    then
        if [ "$1" = "*" ] || [ "$2" = "*" ] || [ "$1" = "./*" ] || [ "$2" = "./*" ]
        then
            echo "Abort: refusing to remove *, please go to the parent folder and do rm &lt;folder_name&gt;/*" 1&gt;&amp;2
            set +f
            return 1
        fi
    fi  
    set +f
    /bin/rm -i $@
}

set +f


I would like to know whether there are any vulnerabilities and whether it can be improved.
The vulnerability I see is that only the first two arguments are checked.  You could check all of them:

rm() { 
    [[ $- == *i* ]] &amp;&amp; for arg
    do 
        if [[ $arg = "*" || $arg = "./*" ]]
        then
            # abort
       fi
   done
   # do the rm


This kind of checking will miss other dangerous wildcards like ** or ?*.  You can get safer checking by expanding * yourself, then see if the expanded arguments contain that same list of files:

# alias not needed here; we want globs to be expanded
rm() {
    declare -a star=(*)
    declare -a dotslashstar=(./*)
    if [[ "$@" == *"${star[@]}"*  ||  "$@" == *"${dotslashstar[@]}"* ]]
    then
         # abort


... but then you can't (for example) empty a directory full of temp files with rm *.tmp, if *.tmp and * match the same thing.