> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to echo a New Line in Bash Shell Scripts
- URL: https://linuxhandbook.com/echo-newline-bash/
- Published: 2022-02-01T11:09:00.000Z
- Updated: 2022-02-28T11:10:00.000Z
- Description: Learn various ways of displaying a new line in the output of echo command in Linux.
- Author: Abhishek Prakash
- Tags: Quick Tip

The [echo command](https://linuxhandbook.com/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 `-e` option:

```
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.

```bash
$ 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.

```bash
$ 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.

```bash
$ 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](https://linuxhandbook.com/bash-variables/).

If you look closely at the snippet below, you will realize that the expansion character, in this case, acts to hold a temporary value.

```bash
$ echo Hello$'\n'world
Hello
world
```

Or, you can use the whole string as a 'temporary variable':

```bash
$ 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](https://linuxhandbook.com/echo-without-newline/)) to the end of your text.

This means that you can chain multiple echo commands together to cause a newline.

```bash
$ echo Hello; echo world
Hello
world
```

## Use printf to print newline in Bash shell

[printf](https://linuxhandbook.com/bash-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.

```bash
$ printf 'Hello\nworld\n'
Hello
world
```

As expected, you have a newline without using any flags.

## Conclusion

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.