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".
|
|||||||
|
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 |
|||||||
|