The Computer Oracle

echo text with new line in bash

Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping

--

Chapters
00:00 Question
00:28 Accepted answer (Score 122)
00:47 Answer 2 (Score 13)
01:29 Answer 3 (Score 4)
02:04 Thank you

--

Full question
https://superuser.com/questions/154936/e...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #bash

#avk47



ACCEPTED ANSWER

Score 127


option 1:

% echo -e "text\n" >> file.conf

option 2:

% ( echo text ; echo "" ) >> file.conf

option 3:

% echo text >> file.conf
% echo ""  >> file.conf



ANSWER 2

Score 13


I think the proper answer should be that your command

echo text >> file.conf

does add an extra line, but after the new text, not before.

I guess that you want to add an extra line before that text, probably because your initial file doesn't end in a new line. In that case you could use

echo -e "\ntext" >> file.conf

instead, as the -e option allows you to use the new line \n character.




ANSWER 3

Score 4


Just to add to akira's response

Option 4:

use ctrl-v ctrl-m key combos twice to insert two newline control character in the terminal. Ctrl-v lets you insert control characters into the terminal. You could use the enter or return key instead of the ctrol-m if you like. It inserts the same thing.

This ends up looking like echo text^M^M >> file.conf