0

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?

Kusalananda
  • 333,661

1 Answers1

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
    echo " what are you" > text1
    
    Content of text1:
    what are you 
    
  • Write more text using >>:
    echo "what are you doing man" >> text1
    
    Content of text1:
    what are you 
    what are you doing man
    

Scenario 2: Text is overwritten

  • Write text to file
    echo "what are you" > text2
    
    Content of text2:
    what are you 
    
  • Write more text, but using >
    echo "what are you doing man" > text2
    
    content of 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