22

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

J.Olufsen
  • 3,492
  • 14
  • 43
  • 59
  • 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 Answers1

44

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

To overwrite bar.txt, use this:

echo "foo" > bar.txt
Dennis
  • 48,917
  • 12
  • 130
  • 149
  • 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](http://superuser.com/questions/277680/is-backwards-redirection-the-same-as-a-pipe). – 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](http://my.safaribooksonline.com/book/-/9781617290213/the-pipeline-connecting-commands/ch04lev1sec3) – 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