Skip to main content
Have another footnote.
Source Link
JdeBP
  • 71.9k
  • 13
  • 175
  • 378

echo "abc" >>file.txt puts a newline after abc, not before. If you end up with abc on its own line, that means that the newline before abc was already present in file.txt.

Note that it is perfectly normal for a text file to end in a newline. On unix, a line consists of a sequence of characters other than null⁰ or newline followed by a newline.1 Therefore any non-empty text file ends with a newline character.

If you want to add text to the last line of a file, then you can't do it with >>, because this always appends to the file, so it always writes after the last newline. Instead you need a tool that is capable of modifying an existing file. For example, you can use sed:

sed '$ s/$/abc/' file.txt >file.txt.new && mv file.txt.new file.txt

In the sed command, the first $ means “do the following command only on the last line”, the command s/REGEX/REPLACEMENT/ replaces REGEX by REPLACEMENT, and the regular expression $ matches at the end of the line.

Linux's sed command has a built-in feature to automate this create-new-file-and-replace sequence, so you can shorten that to

sed -i '$ s/$/abc/' file.txt

That's a null byte, which ASCII calls NUL and Unicode calls U+0000. Text processing programs may or may not cope with this character.
1 See the definitions of Text File, Line, and Newline Character in the "Definitions" section of the Base Definitions chapter of IEEE 1003.1-2008:2016.

echo "abc" >>file.txt puts a newline after abc, not before. If you end up with abc on its own line, that means that the newline before abc was already present in file.txt.

Note that it is perfectly normal for a text file to end in a newline. On unix, a line consists of a sequence of characters other than null⁰ or newline followed by a newline. Therefore any non-empty text file ends with a newline character.

If you want to add text to the last line of a file, then you can't do it with >>, because this always appends to the file, so it always writes after the last newline. Instead you need a tool that is capable of modifying an existing file. For example, you can use sed:

sed '$ s/$/abc/' file.txt >file.txt.new && mv file.txt.new file.txt

In the sed command, the first $ means “do the following command only on the last line”, the command s/REGEX/REPLACEMENT/ replaces REGEX by REPLACEMENT, and the regular expression $ matches at the end of the line.

Linux's sed command has a built-in feature to automate this create-new-file-and-replace sequence, so you can shorten that to

sed -i '$ s/$/abc/' file.txt

That's a null byte, which ASCII calls NUL and Unicode calls U+0000. Text processing programs may or may not cope with this character.

echo "abc" >>file.txt puts a newline after abc, not before. If you end up with abc on its own line, that means that the newline before abc was already present in file.txt.

Note that it is perfectly normal for a text file to end in a newline. On unix, a line consists of a sequence of characters other than null⁰ or newline followed by a newline.1 Therefore any non-empty text file ends with a newline character.

If you want to add text to the last line of a file, then you can't do it with >>, because this always appends to the file, so it always writes after the last newline. Instead you need a tool that is capable of modifying an existing file. For example, you can use sed:

sed '$ s/$/abc/' file.txt >file.txt.new && mv file.txt.new file.txt

In the sed command, the first $ means “do the following command only on the last line”, the command s/REGEX/REPLACEMENT/ replaces REGEX by REPLACEMENT, and the regular expression $ matches at the end of the line.

Linux's sed command has a built-in feature to automate this create-new-file-and-replace sequence, so you can shorten that to

sed -i '$ s/$/abc/' file.txt

That's a null byte, which ASCII calls NUL and Unicode calls U+0000. Text processing programs may or may not cope with this character.
1 See the definitions of Text File, Line, and Newline Character in the "Definitions" section of the Base Definitions chapter of IEEE 1003.1-2008:2016.

Source Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k

echo "abc" >>file.txt puts a newline after abc, not before. If you end up with abc on its own line, that means that the newline before abc was already present in file.txt.

Note that it is perfectly normal for a text file to end in a newline. On unix, a line consists of a sequence of characters other than null⁰ or newline followed by a newline. Therefore any non-empty text file ends with a newline character.

If you want to add text to the last line of a file, then you can't do it with >>, because this always appends to the file, so it always writes after the last newline. Instead you need a tool that is capable of modifying an existing file. For example, you can use sed:

sed '$ s/$/abc/' file.txt >file.txt.new && mv file.txt.new file.txt

In the sed command, the first $ means “do the following command only on the last line”, the command s/REGEX/REPLACEMENT/ replaces REGEX by REPLACEMENT, and the regular expression $ matches at the end of the line.

Linux's sed command has a built-in feature to automate this create-new-file-and-replace sequence, so you can shorten that to

sed -i '$ s/$/abc/' file.txt

That's a null byte, which ASCII calls NUL and Unicode calls U+0000. Text processing programs may or may not cope with this character.