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
- 320,670
- 36
- 633
- 936
Saurav Kumar
- 3
- 1
-
1Read `man bash` and sesrch for "`Redirecting Input`" and "`Redirecting Output`" – waltinator Nov 04 '20 at 05:43
-
3Does this answer your question? [What are the shell's control and redirection operators?](https://unix.stackexchange.com/questions/159513/what-are-the-shells-control-and-redirection-operators) – Vojtech Trefny Nov 04 '20 at 06:03
-
do each of the comands twice and see the result – jsotola Nov 04 '20 at 07:32
-
Thanks for the help. – Saurav Kumar Nov 05 '20 at 01:27
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" > text1text1:what are you - Write more text using
>>:
Content ofecho "what are you doing man" >> text1text1:what are you what are you doing man
Scenario 2: Text is overwritten
- Write text to file
Content ofecho "what are you" > text2text2:what are you - Write more text, but using
>
content ofecho "what are you doing man" > text2text2: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
- 21,637
- 21
- 47
- 71
Rashid Rafiq Dar
- 16
- 2
-
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
-