echo text with new line in bash

I would like to append text to a file. So I wrote in bash

echo text >> file.conf

However it doesn't leave a new line. So I can only do this once. How do I add a new line?

2

3 Answers

option 1:

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

option 2:

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

option 3:

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

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.

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

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like