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.

When using echo "foo">> bar.txt more than once it appends to the end of a file. How to overwrite bar.txt each time?

share|improve this question
2  
Dennis' answer is correct. See man bash and search (using /) for the section on "REDIRECTION". Specifically subsections "Redirecting Output" and "Appending Redirected Output". –  RedGrittyBrick Apr 19 '12 at 18:57

1 Answer 1

up vote 11 down vote accepted

> is for redirecting to a file (overwriting it), while >> is for appending.

To overwrite bar.txt, use this:

echo "foo" > bar.txt
share|improve this answer
1  
More commonly, > is referred to as redirecting (standard output to a file) whilst the pipe symbol | is referred to as piping (standard output to another process). You run the risk of confusing people like me when you refer to > as 'piping' –  RedGrittyBrick Apr 19 '12 at 18:52
    
@RedGrittyBrick: I've seen piping in a few books (and I've been saying it for years), but redirecting seems to be far more common. Thanks. –  Dennis Apr 19 '12 at 18:56
    
@Dennis Piping is using the pipe | for connecting the output of one program/command to the input of another. Related. –  Daniel Beck Apr 19 '12 at 18:58
    
@DanielBeck: I meant I saw piping to a file. Example: Learn Windows PowerShell in a Month of Lunches - Piping to a file or printer –  Dennis Apr 19 '12 at 19:01
    
Note that according to page 43, in PowerShell > foo is just syntactic sugar for | Out-File foo, and therefore is a form of piping ;-) –  Daniel Beck Apr 19 '12 at 19:06

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.