Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Echo newline in Bash prints literal \n

Tags:

bash

echo

newline

In Bash, tried this:

echo -e "Hello,\nWorld!" 

But it doesn't print a newline, only \n. How can I make it print the newline?

I'm using Ubuntu 11.04 (Natty Narwhal).

like image 862
Sergey Avatar asked Dec 11 '11 21:12

Sergey


People also ask

Does echo print a newline?

Note echo adds \n at the end of each sentence by default whether we use -e or not. The -e option may not work in all systems and versions. Some versions of echo may even print -e as part of their output.

How do you skip a new line in echo?

So, if you type in 'echo 1' as the command in your prompt, you'll get 1 as an output, followed by a new line and another input line.

How do you make a new line in shell script?

The most used newline character If you don't want to use echo repeatedly to create new lines in your shell script, then you can use the \n character. The \n is a newline character for Unix-based systems; it helps to push the commands that come after it onto a new line. An example is below.

What is echo $$ in bash?

The echo command is used to display a line of text that is passed in as an argument. This is a bash command that is mostly used in shell scripts to output status to the screen or to a file.


1 Answers

You could use printf instead:

printf "hello\nworld\n" 

printf has more consistent behavior than echo. The behavior of echo varies greatly between different versions.

like image 200
sth Avatar answered Oct 10 '22 11:10

sth