When I do
str="Hello World\n===========\n"
I get the \n printed out too. How can I have newlines then?
|
|
In
Single quotes preceded by a Also
Both solutions do not require a subshell. If in the following you need to print the string, you should use double quotes, like in the following example:
because when you print the string without quotes, newline are converted to spaces. |
|||||||||
|
|
You can put literal newlines within single quotes (in any Bourne/POSIX-style shell).
For a multiline string, here documents are often convenient. The string is fed as input to a command.
If you want to store the string in a variable, use the
In ksh, bash and zsh, you can use the
|
|||||||||||||
|
|
Are you using "echo"? Try "echo -e".
|
|||||||||||||||||||||
|
|
If you need newlines in your script many times you could declare a global variable holding a newline. That way you can use it in double-quoted strings (variable expansions).
|
|||||||||
|
|
From all discussion, here is the simplest way for me:
The echo command must use double quotes. |
|||
|
|
|
To complement the great existing answers: If you're using With trailing (and leading)
With trailing (and leading)
|
|||
|
|
|
you need to do it this way:
Update: As Fred pointed it out, this way you will loose trailing "\n". To assign variable with backslash sequences expanded, do:
let's test it:
gives us now:
Note, that $'' is different than $"". The second does translation according to current locale. For deital see QUOTING section in |
|||||||||
|