Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Echo newline in Bash prints literal \n - Stack Overflow #936

Open
1 task
ShellLM opened this issue Nov 7, 2024 · 1 comment
Open
1 task

Echo newline in Bash prints literal \n - Stack Overflow #936

ShellLM opened this issue Nov 7, 2024 · 1 comment
Labels
forum-discussion Quotes clipped from forums linux Linux notes tools links shell-script shell scripting in Bash, ZSH, POSIX etc

Comments

@ShellLM
Copy link
Collaborator

ShellLM commented Nov 7, 2024

Echo newline in Bash prints literal \n

Snippet

echo -e "Hello,\nWorld!"
Hello,\nWorld!

Full Content

Echo newline in Bash prints literal \n - Stack Overflow

How do I print a newline? This merely prints \n:

$ echo -e "Hello,\nWorld!"
Hello,\\nWorld!

Answers

Use printf instead

printf "hello\nworld\n"

printf behaves more consistently across different environments than echo.

Make sure you are in Bash

$ echo $0
bash

# All these four ways work for me:
echo -e "Hello\nworld"
echo -e 'Hello\nworld'
echo Hello$'\n'world
echo Hello ; echo world

The -e flag "enables interpretation of backslash escapes".

Use $' quoting

echo $'hello\nworld'

prints:

hello
world

The $' strings use ANSI C Quoting.

Use double quotes

echo "hello
world"

This works both inside a script and from the command line. On the command line, press Shift+Enter to do the line break inside the string.

Use a new parameter expansion in Bash 4.4

foo='hello\nworld'
echo "${foo@E}"

This expands the variable with backslash escape sequences.

Simply type echo

echo

to get a new line.

Use single quotes instead of double quotes

echo -e 'Hello,\nWorld!'

This also works, as the behavior of echo can vary across versions and distributions.

Other approaches

There are a number of other techniques shown in the answers, such as using sed, jq, or multiple echo calls. However, the most reliable and portable solutions are using printf or the $' quoting mechanism.

Suggested labels

None

@ShellLM ShellLM added forum-discussion Quotes clipped from forums linux Linux notes tools links shell-script shell scripting in Bash, ZSH, POSIX etc labels Nov 7, 2024
@ShellLM
Copy link
Collaborator Author

ShellLM commented Nov 7, 2024

Related content

#932 similarity score: 0.84
#741 similarity score: 0.84
#924 similarity score: 0.84
#60 similarity score: 0.83
#56 similarity score: 0.83
#62 similarity score: 0.82

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
forum-discussion Quotes clipped from forums linux Linux notes tools links shell-script shell scripting in Bash, ZSH, POSIX etc
Projects
None yet
Development

No branches or pull requests

1 participant