What is the difference between echo hello > a.txt
and echo hello >> a.txt
. It is doing the same thing, why should we use >>
instead of >
?
And what does <
do?
Asked
Active
Viewed 35 times
0

Kusalananda
- 333,661
1 Answers
0
>
and >>
are two different things. If you are writing anything to a file for the first time you use >
and when you want to add more text to the same file without overwriting the already entered text you should use >>
, otherwise using >
will overwrite anything that was written there earlier.
I will show you with an example.
Scenario 1: Text gets appended
- Write text to file
Content ofecho " what are you" > text1
text1
:what are you
- Write more text using
>>
:
Content ofecho "what are you doing man" >> text1
text1
:what are you what are you doing man
Scenario 2: Text is overwritten
- Write text to file
Content ofecho "what are you" > text2
text2
:what are you
- Write more text, but using
>
content ofecho "what are you doing man" > text2
text2
:what are you doing man
The <
on the other hand is an input redirection operator it is used to input a file to any command. For example
cat < file1
can be used to read the contents of a file called file1
. It's the same as
cat file1
Just try them yourself ...

AdminBee
- 22,803
-
This answer needs formatting: read the manual on markdown, or use the formatting buttons. – ctrl-alt-delor Nov 04 '20 at 07:38
-
1
-
2
-
Also, please keep in mind that commands and filenames are case sensitive, so
CAT FILE1
would have just produced an error message ... – AdminBee Nov 04 '20 at 09:18 -
man bash
and sesrch for "Redirecting Input
" and "Redirecting Output
" – waltinator Nov 04 '20 at 05:43