27

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

1
  • 2
    Dennis' answer is correct. See man bash and search (using /) for the section on "REDIRECTION". Specifically subsections "Redirecting Output" and "Appending Redirected Output". Apr 19, 2012 at 18:57

1 Answer 1

49

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

To overwrite bar.txt, use this:

echo "foo" > bar.txt
5
  • 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' Apr 19, 2012 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, 2012 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, 2012 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, 2012 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, 2012 at 19:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .