In Bash, there are multiple ways we can display a text in the console or terminal. We can use either the echo or printf command to print a text. Each of these commands has their unique behaviors.

In this guide, we’ll learn how to print a newline in Bash.

Newline in Bash

Before going further, here’s a quick refresh on what a newline is. It’s usually used to specify the end of a line and to jump to the next line. It’s expressed with the character “n” in UNIX/Linux systems. Most text editors will not show it by default.

Printing Newline in Bash

There are a couple of different ways we can print a newline character. The most common way is to use the echo command. However, the printf command also works fine.

Using the backslash character for newline “n” is the conventional way. However, it’s also possible to denote newlines using the “$” sign.

Printing Newline Using Echo

The echo command takes a string as input and prints it out on the console screen. To print any text, we use the echo command in the following manner:

$ echo “The Quick Brown Fox”

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-789.png" data-lazy- height="131" src="data:image/svg xml,” width=”1091″>

As mentioned earlier, the newline character is “n”, right? How about we try to include it directly with echo?

$ echo “ThenQuicknBrownnFox”

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-790.png" data-lazy- height="125" src="data:image/svg xml,” width=”1091″>

Well, that didn’t go as expected. What happened?

By default, the echo command will print the string provided, character by character. It doesn’t interpret backslash characters. However, we can fix this by adding the flag “-e”. It enables backslash character interpretation. Let’s fix the command and run it again:

$ echo -e “ThenQuicknBrownnFox”

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-791.png" data-lazy- height="203" src="data:image/svg xml,” width=”1094″>

Voila! Now it’s working as expected!

This technique also works when using Bash variables. Take a look at the following example:

$ sentence=“ThenQuicknBrownnFox”

$ echo -e $sentence

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-793.png" data-lazy- height="233" src="data:image/svg xml,” width=”1094″>

Printing Newline Using $

We can also use the “$” sign with the echo command to specify the newline character. This method is a bit more complex than the previous one. The explanation is best done with an example.

Run the following command:

$ echo The$‘n’Quick$‘n’Brown$‘n’Fox

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-795.png" data-lazy- height="207" src="data:image/svg xml,” width=”1092″>

Here,

  • The given string isn’t inside double quotations.
  • Before each newline character “n”, we’re using the “$” sign.
  • Each newline character “n” is provided inside single quote.

Printing Newlines with Multiple Echo Statements

In this approach, we’re basically going to run multiple echo commands instead of one. By default, echo prints the given string and adds a newline character at the end. By running multiple echo statements at once, we’re taking advantage of that.

Let’s have a look at the following example.

$ echo The; echo Quick; echo Brown; echo Fox

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-800.png" data-lazy- height="207" src="data:image/svg xml,” width=”1090″>

Here,

  • We’re running 4 echo commands.
  • Each command is separated by a semicolon (;). It’s the default delimiter in Bash.

Printing Newline with Printf

Similar to echo, the printf command also takes a string and prints it on the console screen. It can be used as an alternative to the echo command.

Have a look at the following example.

$ printf “ThenQuicknBrownnFoxn

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-804.png" data-lazy- height="211" src="data:image/svg xml,” width=”1092″>

As you can see, printf processes backslash characters by default, no need to add any additional flags. However, it doesn’t add an additional newline character at the end of the output, so we have to manually add one.

Final Thoughts

In this guide, we’ve successfully demonstrated how to print newlines in Bash. The newline character is denoted as “n”. Using both the echo and printf commands, we can print strings with new lines in them. We can also cheat (well, technically) by running the same tool multiple times to get the desired result.

For more in-depth info about echo and printf, refer to their respective man pages.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-808.png" data-lazy- height="697" src="data:image/svg xml,” width=”1090″>

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-814.png" data-lazy- height="698" src="data:image/svg xml,” width=”1092″>

Interested in Bash programming? Bash is a powerful scripting language that can perform wonders. Check out our Bash programming section. New to Bash programming? Get started with this simple and comprehensive guide on Bash scripting tutorials for beginners.

Happy computing!

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/profile-photo-1-150×150.jpg" height="112" src="data:image/svg xml,” width=”112″>

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.