Let me clear about newline character:$ echo Hello > file1 ; cat file1Hello$ echo -n  Hello > file2 ; cat file2Hello$ Here you can see that file1 has newline character at the end whereas file2 hasn't. Now suppose  I've one file:$ cat fileHelloWelcome toUnix$And I want to add and Linux at the end of file, then echo  and Linux >> file will be added to newline. But I want last line as Unix and LinuxSo, In order to work around I want to remove newline character at the end of file. Therefore How do I remove the newline character at the end of file?
How do I remove newline character at the end of file?
shell script;text processing;newlines
If all you want to do is add text to the last line, it's very easy with sed. Replace $ (pattern matching at the end of the line) by the text you want to add, only on lines in the range $ (which means the last line).sed '$ s/$/ and Linux/' <file >file.new &&mv file.new filewhich on Linux can be shortened tosed -i '$ s/$/ and Linux/' fileIf you want to remove the last byte in a file, Linux (more precisely GNU coreutils) offers the truncate command, which makes this very easy.truncate -s -1 fileA POSIX way to do it is with dd. First determine the file length, then truncate it to one byte less.length=$(wc -c <file)dd if=/dev/null of=file obs=$((length-1)) seek=1Note that both of these unconditionally truncate the last byte of the file. You may want to check that it's a newline first:length=$(wc -c <file)if [ $length -ne 0 ] && [ -z $(tail -c -1 <file) ]; then  # The file ends with a newline or null  dd if=/dev/null of=file obs=$((length-1)) seek=1fi