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).
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With