标签云

微信群

扫码加入我们

WeChat QR Code

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.


For those saying "it works for me", the behavior of echo varies quite a bit between versions.Some will even print the "-e" as part of their output.If you want predictable behavior for anything nontrivial, use printf instead (as in sth's answer).

2019年05月24日56分44秒

I could not get any of the suggestions in this answer working, because, as it turns out, I was attempting to use it in a function that returns a value, and all the echo (and printf) messages in the function were being appended to the return value after being individually stripped of newlines. Here is a question regarding this, with an extremely thorough answer: stackoverflow.com/questions/27872069/… This was like a three hour mystery tour.

2019年05月24日56分44秒

Also notable: in Unix & Linux Stack Exchange, the accepted answer to How to add new lines when using echo

2019年05月24日56分44秒

GrahamPerrin, thanks for the link to a very good and detailed answer indeed! I think it's better than the answers below.

2019年05月24日56分44秒

echo -ne "hello\nworld" (you needed the n flag to interpret escapes) - but as others say, different echo commands may have different results!

2019年05月24日56分44秒

or even printf %"s\n" hello world -- printf will reuse the format if too many arguments are given

2019年05月24日56分44秒

The OP asked about echo, not printf; and choroba's answer below, which uses the -e option, fills the bill perfectly.

2019年05月24日56分44秒

JESii: It fits if your echo happens to support the -e option.

2019年05月24日56分44秒

With some versions of echo, -e is just printed in the output itself so I think this answer is perfectly valid since echo isn't consistent here (unless we're talking about a specific version).

2019年05月24日56分44秒

This is well and good if printf is available, but unlike echo sometimes printf isn't on the distro.

2019年05月24日56分44秒

-e flag did it for me, which "enables interpretation of backslash escapes"

2019年05月24日56分44秒

I think -e param doesn't exist on all *nix OS

2019年05月24日56分44秒

kenorb: It exists in bash. It is a builtin.

2019年05月24日56分44秒

Why does the third one work? Without the $ it returns "Hello n world"

2019年05月24日56分44秒

As mentioned by various other -e does NOT work for all distributions and versions.In some cases it is ignored and in others it will actually be printed out.I don't believe this fixed it for the OP so should not be accepted answer

2019年05月24日56分44秒

Does this not work for anybody? Let us know.

2019年05月24日56分44秒

EvgeniSergeev Not sure what you mean, but it didn't work for me either first. And that's because I was using double quotes and turns out this works only with single quotes! Tried in Terminal on Mac.

2019年05月24日56分44秒

Problems with variables in the string not being expanded.

2019年05月24日56分44秒

echo "" works for me and I think it's the simplest form to print a new line, even if this doesn't directly answer the question. Cheers.

2019年05月24日56分44秒

I think it's less obvious (and thus potentially more confusing) than echo -en "\n".

2019年05月24日56分44秒

echo is enough to obtain an empty line

2019年05月24日56分44秒

echo '' doesn't work btw

2019年05月24日56分44秒

Okneloper Works just fine for me.

2019年05月24日56分44秒

Be sure you wrap the variable with quotes before echoing it out of the method.

2019年05月24日56分44秒

Works for me in GitBash on Windows 7, too ;-)

2019年05月24日56分44秒

A single backslash should be enough

2019年05月24日56分44秒

Minus one for repeating already existing answer.

2019年05月24日56分44秒

This is actually a great answer since it works for string concatenations. Great!

2019年05月24日56分44秒

Why bother to invoke a second program? It's not that we are trying to write a real time application in bash ;) but its not necessary.

2019年05月24日56分44秒

this is really a great answer which should work everywhere no matter what. You saved my day friend alinsoar

2019年05月24日56分44秒

How do you type that line break without having the shell to run the command when you press Ènter`?

2019年05月24日56分44秒

Xenos This works well in a script. And on my macOS, I just click on Shift+Enter and it works.

2019年05月24日56分44秒

Just a note, you will probably want to use ${ } around your variable names as not doing so can lead to really weird behavior whena shell finds a variable called $warningsFound and prints that and not the two separate outputs.

2019年05月24日56分44秒

dragon788 maybe I'm missing something, but the variable IS actually called $warningsFound ?

2019年05月24日56分44秒

I missed a word on that. If you had a variable called $warnings, in some cases without using ${warningsFound}, you could potentially end up with the contents of $warnings + "Found" instead of the variable you intended.

2019年05月24日56分44秒

echo -ne "hello\nworld"for the exact answer of the question :)

2019年05月24日56分44秒

syntax error near unexpected token `(' when called in .sh file

2019年05月24日56分44秒

try echo hello; echo world

2019年05月24日56分44秒

Or "echo hello && echo world" or just:" echo hello echo world

2019年05月24日56分44秒

This is incorrect. It is never interpreted as \n. It is interpreted as a separate argument to the program, and the program itself may display that argument on a new line, but that doesn't mean that it was converted to \n at any point and is entirely dependent on the program.

2019年05月24日56分44秒