When using echo "foo">> bar.txt more than once it appends to the end of a file. How to overwrite bar.txt each time?
Asked
Active
Viewed 8.1k times
27
J.Olufsen
- 3,682
1 Answers
49
> is for redirecting to a file (overwriting it), while >> is for appending.
To overwrite bar.txt, use this:
echo "foo" > bar.txt
Dennis
- 49,727
-
1More 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
> foois just syntactic sugar for| Out-File foo, and therefore is a form of piping ;-) – Daniel Beck Apr 19 '12 at 19:06
man bashand search (using/) for the section on "REDIRECTION". Specifically subsections "Redirecting Output" and "Appending Redirected Output". – RedGrittyBrick Apr 19 '12 at 18:57