Search code examples
linuxbashaliaszshoh-my-zsh

Different rm -f behavior between bash and oh-my-zsh


I am using oh-my-zsh. I have both the following two aliases about rm command get sourced:

alias rm='rm -i'  

function clean_tex_mid_file()
{
    rm -f *.dvi *.log *.synctex.gz *.aux
}
alias cleantex='clean_tex_mid_file'

And I found that when I tried to remove files not exist using cleantex defined above, it complained like 'no matches found: *.dvi', ignoring the -f option.

A deeper test shows bash will keep silent trying to delete files not exist, sourcing the same alias file.

So how to handle this problem and what happened behind?


Solution

  • zsh, by default, fails if running a command with a glob with no matches. This behavior contravenes the POSIX sh standard.

    bash, by default, follows the POSIX standard, which requires passing the original glob expression to the application in question. Because rm -f is specified to treat the case where a file already doesn't exist identically to the case where it successfully deleted that file, a glob expression with no matches is thus treated as a successful case.

    To change this behavior on bash, run:

    # tell bash to abort a command rather than passing it an unexpanded glob
    shopt -s failglob