Echo newline in Bash prints literal \n

new line in shell script echo
bash newline in variable
echo new line batch
bash newline in string
new line in linux terminal
echo new line php
echo parse newline
alpine 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.


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.

How to echo newline in Linux/Unix shell, with the '\n' character, you must be in bash shell. printf's %b format specifier was meant specifically to replace echo -e (actually, the XSI extension to echo which calls for special interpretation of the arguments by default. -e was never specified and is disallowed by POSIX.), and is identical in virtually every way including a few differences from $'' and the format string argument to printf .


Are you sure you are in bash? Works for me, all three ways:

echo -e "Hello\nworld"
echo -e 'Hello\nworld'
echo Hello$'\n'world

How to add new lines when using echo, builtin expand backslash-escaped characters by default, without requiring the -e option. Newline in Echo: When to use -n, when to use \c. echo: -n or \c. Fortunately, this is becoming less of a problem, but it is worth being aware of, particularly if you are writing scripts for older Unix systems. When you use the echo statement, a newline is added at the end of the command.


echo $'hello\nworld'

prints

hello
world

$'' strings use ANSI C Quoting:

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

echo text with new line in bash, control characters into the terminal. You could use the enter or return key instead of the ctrol-m if you like. This doesn't actually embed newlines, it embeds \n, which the echo -e converts to newlines as it prints. Depending on your actual goal, this may or may not do the trick. – Gordon Davisson Feb 4 '12 at 17:52. Thank you very much @GordonDavisson.


You could always do echo "".

e.g.

echo "Hello"
echo ""
echo "World"

How to input / start a new line in bash terminal?, How do you go to the next line in terminal? Windows batch: echo without new line. Ask Question Asked 8 years, 8 months ago. Active 4 months ago. Echo newline in Bash prints literal . Hot Network Questions


Try

echo -e "hello\nworld"
hello
world

worked for me in nano editor.

From the man page:

-e enable interpretation of backslash escapes

Using Newline n in Bash Shell Scripts, An echo implementation which strictly conforms to the Single Unix And so there really isn't any general way to know how to write a newline with echo That prints a literal \n at the tail of the arguments if the last argument  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 Snapshot created" However, this is not portable, as the -e flag doesn't work consistently in all systems


How to print new line character with echo?, Echo newline in bash shell prints literal n but not newline. Printing literal 'n' in nested print new line in bash scripts.How can I print a newline as  How can you you insert a newline from your batch file output? I want to do something like: Came in useful for me. I had to do echo \n \n | my_app.exe in a script. I did (echo. && echo.) | my_app.exe – Vignesh Feb 18 '11 at 9:54. You can insert an invisible ascii chr (255) on a separate line which will force a blank new line.


Newline in Echo: When to use -n, when to use \c, The new line character with the echo command is "\n". Taking the following example: echo -e "This is line 1\nThis is line 2". Would result in the output. This is line  Uses of in Bash (Line Feed) is used as a newline character for Unix based systems. Below is a simple example to use newline character in bash shell scripts. Use one of followings examples. String in double quote: echo -e "This is First Line This is Second Line" String in single quote: echo -e 'This is First Line This is Second Line'


Strip out the 'n' \ new line out of the echo to a file, option 1: % echo -e "text\n" >> file.conf. option 2: % ( echo text ; echo "" ) >> file.​conf. option 3: % echo text >> file.conf % echo "" >> file.conf. Suppose that you want to mostly not echo commands inside your script during execution (e.g. to not bog the user down with details that they do not care about) but that there is the occaisional script line that you would like to echo before you execute it.