If a file exists, >
will overwrite the file and >>
will append to the end of the file. If a file does not exist, both will create a new file.
Also, the echo
command automatically adds a newline after your string.
So
echo 1 > num.txt
echo 1 > num.txt
echo 2 >> num.txt
will create the following file:
1
2
Not this:
1 1 2
or
1 2
Furthermore, you cannot just modify a single line in a text file. You have to read the whole file, modify it in your code and then write to the whole file again.