How to echo a New Line in Bash Shell Scripts
Learn various ways of displaying a new line in the output of echo command in Linux.
— Team LHB
The echo command automatically adds a new line at the end. That's cool.
But what if you want to display just an empty new line? Or if you want to output something that contains a new line?
The good news is that, echo lets you use the newline character
\n to print a new line within the same output line if you use the
echo -e "Name\nAddress\nPhone Number"
If you run the above command, you'll get this output:
Name Address Phone Number
That's nice, right? Let's have a more detailed look into it.
Display new line with -e flag of echo command (recommended)
A newline is a term we use to specify that the current line has ended and the text will continue from the line below the current one. In most UNIX-like systems,
\n is used to specify a newline. It is referred to as newline character.
The echo command, by default, disables the interpretation of backslash escapes. So if you try to display a newline using the '\n' escape sequence, you will notice a problem.
$ echo Hello\nworld Hellonworld $ echo 'Hello\nworld' Hello\nworld
Enclosing text in single quotes as a string literal does not work either.
That was not an expected output. To actually print a new-line, you can use the '-e' flag to tell the echo command that you want to enable the interpretation of backslash escapes.
$ echo -e 'Hello\nworld' Hello world
Nice, that's what you are looking for.
Let me some other ways to display the newline character.
echo a variable containing new line
You can store a string in a bash variable and then echo it using the '-e' flag.
$ str="Hello\nworld" $ echo -e $str Hello world
Use the '$' character instead of -e flag
The dollar symbol, '$' is called the "expansion" character in bash. This is the character that I used in the earlier example to refer to a variable's value in shell.
If you look closely at the snippet below, you will realize that the expansion character, in this case, acts to hold a temporary value.
$ echo Hello$'\n'world Hello world
Or, you can use the whole string as a 'temporary variable':
$ echo $'Hello\nworld' Hello world
I would prefer to use the
-e flag, though.
echo your echo to print something with new line
When you echo a piece of text, the echo command will automatically add a newline (and here is how you can prevent it) to the end of your text.
This means that you can chain multiple echo commands together to cause a newline.
$ echo Hello; echo world Hello world
Use printf to print newline in Bash shell
printf is another command line tool that essentially prints text to the terminal, but it also allows you to format your text.
The usage is very simple and similar to echo but a bit more reliable and consistent.
$ printf 'Hello\nworld\n' Hello world
As expected, you have a newline without using any flags.
Personally, I would prefer sticking with the -e flag or go for the printf command for displaying the new lines in output. I recommend you to do the same but feel free to experiment.