1

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?

dlmeetei
  • 9,905
  • 3
  • 31
  • 38
lincr
  • 1,633
  • 1
  • 15
  • 36
  • `rm -f` is behaving to spec here -- it's **supposed** to silently succeed when something doesn't exist. – Charles Duffy Aug 21 '17 at 17:47
  • 1
    Closely related: [Why zsh tries to expand * and bash does not?](https://stackoverflow.com/questions/20037364/why-zsh-tries-to-expand-and-bash-does-not) – Charles Duffy Aug 21 '17 at 17:52
  • Note that using an alias to give a simpler name to a function isn't necessary in `zsh`; you can give a function multiple names: `clean_tex_mid_file cleantex () { ... }`. It's not clear why you need multiple names, though, instead of just naming the function `cleantex` in the first place. – chepner Aug 25 '17 at 00:24
  • @chepner Thanks for your helpful advice, not knowing this trick before. The reason using of two names is that I think function name should be de detailed and descriptive and alias name should be short, though not so different for this example. – lincr Aug 25 '17 at 09:05

1 Answers1

2

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
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441