Bash – Are there any disadvantages of setting `noclobber`

bashshellzsh

Given that zsh can clobber all files given the command:

>*

I'm thinking that setting the option noclobber would be a good idea.

I can always use >| file if I want to use the default clobber behaviour in both bash and zsh. (zsh also allows the alternative syntax >!file).

I'm guessing noclobber is unset by default because of POSIX compatibility, but just to be sure:

Are there any downsides to setting noclobber?

Is there anyway to set noclobber only for the interactive shell?

Best Answer

The reason noclobber is not set by default is tradition. As a matter of user interface design, it's a good idea to make “create this new file” the easy action and to put an extra hurdle the more dangerous action “either create a new file or overwrite an existing file”. Thus noclobber is a good idea (> to create a new file, >| to potentially overwrite an existing file) and it would likely have been the default if the shell had been designed a few decades later.

I strongly recommend to use the following in your interactive shell startup file (.bashrc or .zshrc):

set -o noclobber
alias cp='cp -i'
alias mv='mv -i'

In each case (redirection, copying, moving), the goal is to add an extra hurdle when the operation may have the side effect of erasing some existing data, even though erasing existing data is not the primary goal of the operation. I don't put rm -i in this list because erasing data is the primary goal of rm.

Do note that noclobber and -i are safety nets. If they trigger, you've done something wrong. So don't use them as an excuse to not check what you're overwriting! The point is that you should have checked that the output file doesn't exist. If you're told file exists: foo or overwrite 'foo'?, it means you made a mistake and you should feel bad and be more careful. In particular, don't get into the habit of saying y if prompted to overwrite (arguably, the aliases should be alias cp='yes n | cp -i' mv='yes n | mv -i', but pressing Ctrl+C makes the output look better): if you did mean to overwrite, cancel the command, move or remove the output file, and run the command again.

It's also important not to get into the habit of triggering those safeties because if you do, one day you'll be on a machine which doesn't have your configuration, and you'll lose data because the protections you were counting on aren't there.

noclobber will only be set for interactive shells, since .bashrc or .zshrc is only read by interactive shells. Of course you shouldn't change shell options in a way that would affect scripts, since it could break those scripts.