13.6 Safe I/O Redirection with noclobberHave you ever destroyed a file accidentally? If you set the noclobber C shell variable, or the noclobber option in bash and ksh , it can help you avoid these mistakes. Setting noclobber prevents you from destroying a file when you are redirecting standard output ( 13.1 ) . Consider the following situation:
%
The command above destroys the old
outputfile
and creates a new
one. If you have misspelled the name of your output file, or if you
have forgotten that the file already exists and contains important
data, or (most common) if you really meant to type
Setting the variable
noclobber
prevents this problem. If
noclobber
exists, the C shell will not allow I/O redirection to
destroy an existing file, unless you explicitly tell it to by adding an
exclamation point (
%
Be sure to put space after the Remember that noclobber is not an environment variable, so any new shells you create won't inherit it ( 6.8 ) . Therefore, if you want this feature, put the set command (above) in your shell's setup file ( 2.2 ) .
The C shell noclobber variable has one other feature that's worth noting. Normally, the C shell lets you append to a file that doesn't exist. If noclobber is set under csh and tcsh , it won't; you can only append to files that already exist unless you use an exclamation point:
% - , |
|