Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

The bash noclobber option prevents one from overwriting files with redirection. But sometimes I really want to. csh has a similar option and it can be over-ridden by putting a ! before the filenames. Is there any way to do this with bash?

share|improve this question
add comment

2 Answers

up vote 11 down vote accepted

Yes. Append | to the redirection operator to form >|. This is in § 3.6.2 of the Bash Reference Manual, q.v.

If the redirection operator is ‘>’, and the noclobber option to the set builtin has been enabled, the redirection will fail if the file whose name results from the expansion of word exists and is a regular file. If the redirection operator is ‘>|’, or the redirection operator is ‘>’ and the noclobber option is not enabled, the redirection is attempted even if the file named by word exists.

Visit this tutorial about the noclobber option. It might be of help.

share|improve this answer
    
Thanks! I wish they had used the same syntax... –  vy32 Nov 6 '11 at 12:32
add comment

Use | ("pipe") in bash, instead of ! ("bang") as you would in csh. For example:

echo done >| out

Note that if you want to redirect stderr to stdout and override noclobber, neither >|& nor &>| will work; use the long form instead:

cmd-with-errors done >| out 2>&1
share|improve this answer
    
Do you know why err &>| out doesn't work? –  Sangdol Oct 23 '13 at 13:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.