Thursday, November 13, 2008

bash: defend yourself from overwriting files

I suppose almost everybody put '>' instead of '>>' to redirect the output to file.
In bash it's possible to set noclobber option to avoid file overwriting.

$touch test
$set -o noclobber
$echo test > test
bash: test: cannot overwrite existing file
If you really know what you are doing you you can use '>|' to overwrite the file successfully.
$set -o | grep noclobber
noclobber       on
$echo test >| test
$cat test
test
Very useful option and I think it should be on by default.

No comments: