Append to end to line without newline

bashcommand lineechosed

When i try to append to the end of a file it creates a new line. Now I have tried to fix this with echo -n but that doenst work.

So what am I trying. I'm trying to get the following result:

Hello
This
Is
A
Test

But when I Append text with echo with

echo -n "Test2" >> file.txt

The following thing happens:

Hello
This
Is
A
Test
Test2

But what I want is:

Hello
This
Is
A
Test Test2

How am I able to do this? I have tried sed,echo and printf but none of these give the right result

Best Answer

With sed:

sed '$s/$/ Test2/' file

or

truncate -s-1 file
echo -n " Test2" >> file

Output:

Hello
This
Is
A
Test Test2
Related Question