137

I want to insert a new line between multiple echo statements. I have tried echo "hello\n", but it is not working. It is printing \n. I want the desired output like this:

Create the snapshots

Snapshot created
Michael
  • 7,248
  • 5
  • 56
  • 81
user3086014
  • 3,933
  • 5
  • 26
  • 52

4 Answers4

236

The simplest way to insert a new line between echo statements is to insert an echo without arguments, for example:

echo Create the snapshots
echo
echo Snapshot created

That is, echo without any arguments will print a blank line.

Another alternative to use a single echo statement with the -e flag and embedded newline characters \n:

echo -e "Create the snapshots\n\nSnapshot created"

However, this is not portable, as the -e flag doesn't work consistently in all systems. A better way if you really want to do this is using printf:

printf "Create the snapshots\n\nSnapshot created\n"

This works more reliably in many systems, though it's not POSIX compliant. Notice that you must manually add a \n at the end, as printf doesn't append a newline automatically as echo does.

janos
  • 115,756
  • 24
  • 210
  • 226
  • 1
    but what would be the equivalent for `echo "i want to be on one line" echo "i want to be on another"` by using one echo command, and just \n, so that when you have long sentences, you can wrap your text in bash(using maybe 3 or 4 bash lines atleast with only one echo command. I came here hoping to find that answer. e.g. elaborate your final answer to make it multiple lines with one echo command, what if i wanted to cut it off right at `Snapshot created` on my bash file and make that a new bash line, without adding echo command? I think use plus `+` somehow right? – blamb Sep 07 '16 at 18:10
  • 1
    @blamb you can break a line using `\` at the end of a line. But that's a very different topic from what was asked here. – janos Oct 28 '16 at 11:10
79

Use this echo statement

 echo -e "Hai\nHello\nTesting\n"

The output is

Hai
Hello
Testing
Kalanidhi
  • 4,462
  • 24
  • 42
24
echo $'Create the snapshots\nSnapshot created\n'
serenesat
  • 4,596
  • 10
  • 34
  • 52
Ohad Cohen
  • 5,100
  • 3
  • 33
  • 34
15

You could use the printf(1) command, e.g. like

printf "Hello times %d\nHere\n" $[2+3] 

The  printf command may accept arguments and needs a format control string similar (but not exactly the same) to the one for the standard C printf(3) function...

default
  • 10,975
  • 8
  • 63
  • 100
Basile Starynkevitch
  • 216,767
  • 17
  • 275
  • 509