1. Overview

We can imagine cases where we need to insert newlines into a variable. For example, for any tasks that require text formatting, generate a list of commands to run.

For this reason, in this tutorial, we’ll illustrate examples of how to insert newlines to a variable in bash.

2. Displaying Already Formatted Text

First, we would like to show briefly what are the possibilities of displaying text with newlines.

Let’s imagine that we have text with newlines characters. Given that let’s try to use echo:

$ text="first \nsecond \nthird"
$ echo $text
first \nsecond \nthird

As we can see, the \n sequence was not recognized as a new line.

2.1. echo -e Command

To enable recognition of special character sequences like newline \n we can use echo with the special option -e:

$ text="first \nsecond \nthird"
$ echo -e $text
first 
second
third

This time we have achieved what we wanted and the words are in new lines.

2.2. printf Command

With printf we can accomplish the same result as we did with echo -e:

$ text="first \nsecond \nthird"
$ printf %b $text
first 
second
third

2.3. Text With New Lines

Let’s consider the situation when we have a text with new lines:

$ text="first
second
third"
$ echo "$text"
first 
second
third

As we can see when we deal with the text that contains inserted newlines we can also display text as we wanted. However, let’s notice that we have to wrap the variable with parenthesis otherwise it wouldn’t display the text with new lines.

2.4. Shell Parameter Expansion

Additionally, we can manipulate the text with parameter expansion.

For this purpose, we need to add $ operator before all occurrences of \n:

$ text="first "$'\n'"second "$'\n'"third"
$ echo "$text"
first 
second 
third

3. Automatically Adding Newlines to the Text

In the previous section, we have shown how to display and format text to print new lines. However, in real examples, it might be very useful to have the ability to preprocess texts and add newlines accordingly to specified rules. Because of it, we have prepared a few examples of how to set new lines in text.

3.1. Sequentially Adding Newlines

Let’s assume that we want to insert newlines instead of spaces in the text that we were previously using as an example. Therefore we can for instance iterate over each world and insert one of the special sequence characters that we already know from the previous section:

$ text="first second third"
$ for word in $text
$ do
$  p+="$word"$'\n'
$ done 
$ echo "$p"
first 
second 
third

Great as we can see, it works!

3.2. Adding Newlines Instead of the Selected Character

But what if we would like to add a newline instead of “;” or any other characters than spaces? For this reason, we have to modify a little bit our previous script:

$ text="first ; second ; third"
$ IFS=";"
$ for word in $text; do
>  p+="$word"$'\n'
> done 
$ echo "$p"
first 
second 
third

Perfect! So now we know how to store newlines in variables and also to manipulate any text or sequence of commands to insert newlines characters wherever we want.

3.3. Using the tr Command

Instead of iterating over all words in the text, we can use something that would be handier. So that tr would be a perfect choice for our purposes:

$ text="first second third"
$ echo $text | tr " " "\n"
first
second
third

Great! The result is the same as it was when we have used the loop, but this time we have fit everything in one line.

4. Assigning to a Variable a Text from a File

Now let’s say that we would like to use the text that is stored in the file. For this reason, nothing stands in the way to use previously presented commands. However, when working with text from files the first thing that comes to our mind is the sed command.

4.1. Using the sed Command

Of course, the sed command combined with echo could be used also with examples where we used the text assigned to variables. But to make it elegant without the need for writing more than one line of code we prepared examples with sed:

$ cat text.txt 
one two three four five six
$ text=$(sed 's/ /\n/g' text.txt)
$ echo $text
one
two
three
four
five
six

Awesome with only one line of code we can read the text from a file, insert newlines instead of the spaces (of course we can exchange any character to the newline), and assign it to a variable.

4.2. awk Command

We assume that is always better to have more options. With this in mind, we would like to present one more example of inserting newlines into a variable:

$ cat text.txt 
one two three four five six
$ text=$(awk '{gsub(/ /,"\n")}1' text.txt)
$ echo $text
one
two
three
four
five
six

It does pretty much the same job as sed but awk has a lot of additional features, which might be very useful for us.

5. Conclusion

In this article, we have started with a presentation of what are the options to display variables with newlines. Then we have presented multiple ways of inserting newlines sequence into a variable. Additionally, we also illustrated how to replace different characters into newlines character sequences.

Finally, we have shown how to process and format text from files and then assign it to a variable.