How to add newlines into variables in bash script

When I do

str="Hello Worldn===========n"

I get the n printed out too. How can I have newlines then?

Asked By: Jiew Meng

||

Are you using “echo”? Try “echo -e”.

echo -e "Hello Worldn===========n"
Answered By: Mike

you need to do it this way:

STR=$(echo -ne "Hello Worldn===========n")

Update:

As Fred pointed it out, this way you will loose trailing “n”.
To assign variable with backslash sequences expanded, do:

STR=$'Hello Worldn===========nn'

let’s test it:

echo "[[$STR]]"

gives us now:

[[Hello World
===========

]]

Note, that $” is different than $””. The second does translation according to current locale. For deital see QUOTING section in man bash.

Answered By: Michał Šrajer

In bash you can use the syntax

str=$'Hello Worldn===========n'

Single quotes preceded by a $ is a new syntax that allows to insert escape sequences in strings.

Also printf builtin allows to save the resulting output to a variable

printf -v str 'Hello Worldn===========n'

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:

echo "$str"

because when you print the string without quotes, newline are converted to spaces.

Answered By: enzotib

You can put literal newlines within single quotes (in any Bourne/POSIX-style shell).

str='Hello World
===========
'

For a multiline string, here documents are often convenient. The string is fed as input to a command.

mycommand <<'EOF'
Hello World
===========
EOF

If you want to store the string in a variable, use the cat command in a command substitution. The newline character(s) at the end of the string will be stripped by the command substitution. If you want to retain the final newlines, put a stopper at the end and strip it away afterward. In POSIX-compliant shells, you can write str=$(cat <<'EOF'); str=${str%a} followed by the heredoc proper, but bash requires the heredoc to appear before the closing parenthesis.

str=$(cat <<'EOF'
Hello World
===========
a
EOF
); str=${str%a}

In ksh, bash and zsh, you can use the $'…' quoted form to expand backslash escapes inside the quotes.

str=$'Hello Worldn===========n'

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

NL=$'n'
str="Hello World${NL} and here is a variable $PATH ===========${NL}"
Answered By: pihentagy

From all discussion, here is the simplest way for me:

bash$ str="Hello World
==========="
bash$ echo "$str"
Hello World
===========

The echo command must use double quotes.

Answered By: kholis

To complement the great existing answers:

If you’re using bash and you prefer using actual newlines for readability, read is another option for capturing a here-doc in a variable, which (like other solutions here) doesn’t require use of a subshell.

# Reads a here-doc, trimming leading and trailing whitespace.
# Use `IFS= read ...` to preserve it (the trailing n, here).
read -r -d '' str <<'EOF'   # Use `IFS= read ...` to preserve the trailing n
Hello World
===========
EOF
# Test: output the variable enclosed in "[...]", to show the value's boundaries.
$ echo "$str"
[Hello World
===========]
  • -r ensures that read doesn’t interpret the input (by default, it would treat backslashes special, but that is rarely needed).

  • -d '' sets the “record” delimiter to an empty string, causing read to read the entire input at once (instead of just a single line).

Note that by leaving $IFS (the internal field separator) at its default, $' tn' (a space, a tab, a newline), any leading and trailing whitespace is trimmed from the value assigned to $str, which includes the here-doc’s trailing newline.
(Note that even though the here-doc’s body starts on the line after the start delimiter ('EOF' here), it does not contain a leading newline).

Usually, this is the desired behavior, but if you do want that trailing newline, use IFS= read -r -d '' instead of just read -r -d '', but note that any leading and trailing whitespace is then preserved.
(Note that prepending IFS=  directly to the read command means that the assignment is in effect during that command only, so there is no need to restore the previous value.)


Using a here-doc also allows you to optionally use indentation to set off the multiline string for readability:

# Caveat: indentation must be actual *tab* characters - spaces won't work.
read -r -d '' str <<-'EOF' # NOTE: Only works if the indentation uses actual tab (t) chars.
    Hello World
    ===========
EOF
# Output the variable enclosed in "[...]", to show the value's boundaries.
# Note how the leading tabs were stripped.
$ echo "$str"
[Hello World
===========]

Placing - between << and the opening here-doc delimiter ('EOF', here) causes leading tab characters to be stripped from the here-doc body and even the closing delimiter,
but do note that this only works with actual tab characters, not spaces, so if your editor translates tab keypresses into spaces, extra work is needed.

Answered By: mklement0
#!/bin/bash

result=""

foo="FOO"
bar="BAR"

result+=$(printf '%s' "$foo")$'n'
result+=$(printf '%s' "$bar")$'n'

echo "$result"
printf '%s' "$result"

output:

FOO
BAR

FOO
BAR
Answered By: wuan

The first comment on the question mentions arrays, but nobody shows how to do it with arrays, so here it is.

str=( "Hello World" )
str+=(===========)
printf '%sn' "${str[@]}"

The first line creates an array with one element. Here the quotes are necessary (single quotes work too), otherwise the two words would be added as two separate array elements.

The second line adds one more element to the array with the += operator. Here the quotes are optional, because there is no whitespace.

The third line prints out each array element as string (%s) followed by a newline (n).

There can be whitespace around the array elements, on the first line I added spaces between the string and brackets, on the second I omitted spaces, to show it works both ways.

Answered By: Paul Tobias
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.