Last night, i build some script in sh to change an existing value from a few files.

Let say, i have this command:

echo "Hai" > outputfile.txt

If the file outputfile.txt isn’t already exist, the command will be executed successfully.

But, if the outfile.txt was an existing file, we will got an error zsh: file exists: outputfile.txt. (zsh is my current shell)

And of course, i got the very same error last night because i wanted to change a value from an existing files.

So i search around the world of internet (LOL), and i found that it called NoClobber in shell world.

NoClobber is an option that we can use to disallow an operation to overwriting any existing files. Above command was the example.

So, how this is actually work? or how can it be enabled?

Well, there’s a simple way to turn on the NoClobber option. With command:

set -o noclobber

Our existing shell session will not allow any overwriting operation to any files.

What if we wanted to disable it? Simply execute this command:

set +o noclobber

Above command will turning off the noclobber option on our current shell session. With that, we can continue or repeat our last operation which is in my case is changing existing file with a new value.

But, if you don’t wanted to disable it permanently and just for this spesific operations we can use this command:

echo "Hai" >! outputfile.txt

I don’t exactly understand how it works (yet), but i found it helpfull for my problem last night and of course i don’t want to disable the NoClobber option because i think its a good thing for disallow overwriting existing files.

If you want to enable NoClobber on your all shell session, simply put:

### if you are using bash
echo 'set -o noclobber' >> ~/.bash_profile

### if you are using zsh
echo 'set -o noclobber' >> ~/.zshrc