143

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
  • 8,362
  • 6
  • 61
  • 88
user3086014
  • 4,241
  • 5
  • 27
  • 56

4 Answers4

242

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
  • 120,954
  • 29
  • 226
  • 236
  • 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
84

Use this echo statement

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

The output is

Hai
Hello
Testing
Kalanidhi
  • 4,902
  • 27
  • 42
27
echo $'Create the snapshots\nSnapshot created\n'
serenesat
  • 4,611
  • 10
  • 37
  • 53
Ohad Cohen
  • 5,756
  • 3
  • 39
  • 36
17

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
  • 11,485
  • 9
  • 66
  • 102
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547