Preserve Linebreaks When Storing Command Output to a Variable

In Linux, command substitution allows a user to use the output of a command as an argument for other commands. Here you can substitute all commands that can write standard output. Moreover, you can use the command substitution to create a shell script and save the command's output as a variable.

Some of the output consists of multiple lines, so the output may not appear in the same pattern when you perform command substitution. When you don't use the proper shell variable, the output shows in a single line, i.e., without preserving the linebreaks. In this tutorial, we will look at preserving linebreaks when storing command output in variables through methods like double quotes, here-string, and shell script.

The Problem Lost Linebreaks

Here we will assign the output of a file to a shell variable using command substitution. For example, we have a Linux.txt file, but first let's check the cat command to see the content of this file:

:~$ cat Linux.txt
Some popular Linux Distros are:
Ubuntu
Centos
Arch Linux
Rocky Linux
Alpine Linux

You can see that the output of the above file has multiple linebreaks. Now, we will use the command substitution to assign the output obtained above.

After substituting the variable in the shell, see the output of the var variable with the following echo command.

:~$ var=$(cat Linux.txt) 
:~$ echo $var 
Some popular Linux Distros are: Ubuntu Centos Arch Linux Rocky Linux Alpine Linux

You can see that instead of displaying the output on separate lines, it shows them together. We have to use these shell variables slightly differently to preserve linebreaks when storing the output to a variable.

Methods to Preserve Linebreaks

Double Quotes Method

You can preserve linebreaks in the output by enclosing the variable in double quotes.

:~$ echo "$var"
Some popular Linux Distros are:
Ubuntu
Centos
Arch Linux
Rocky Linux
Alpine Linux

Hence, quoting the variable in double quotes is the best approach to preserve the linebreaks.

In case you add single quotes rather than double, the system treats the variable as a single argument and prints the same. That's why you should always use double quotes when quoting shell variables.

Here-String Method

Here-string is used to redirect input from a variable or path. This method is much preferred when storing command output to a variable quickly. You have to use the <<< operator in the here-string to redirect the string to the command.

command <<<$variable

For example, we will use the above example and store the linebreaks through the here-string.

:~$ cat <<<$var
Some popular Linux Distros are:
Ubuntu
Centos
Arch Linux
Rocky Linux
Alpine Linux

Shell Script Method

You can also print the output, maintaining the linebreaks while storing the command by creating a shell script. Here we will create this shell script using a concept of for loop and the IFS shell variable (Internal Field Separator).

For this, we will be passing the file's name as a command line argument, and with that filename, the script should be able to print the file's content and preserve the linebreaks.

In the below shell script, we create a variable using the filename and assign it to the value of ${1}. The system will pass the value as a command line argument. As you already know, all the lines are terminated with the
character in Linux. So all linebreaks are maintained with the line feed character. Hence we define this character with the IFS variable.

#!/bin/bash
file=${1}
IFS=$'
' for line in $(cat ${file}) do echo "${line}" done

Save the above-created shell script and exit. Now, you have to write the file name with this shell script and run it whose output linebreaks you want to continue.

:~$ ./Linux.sh Linux.txt
Some popular Linux Distros are:
Ubuntu
Centos
Arch Linux
Rocky Linux
Alpine Linux

Comparison of Methods

Method Syntax Advantages Use Case
Double Quotes echo "$var" Simple, most common Best for general use
Here-String cat <<<$var Clean syntax for redirections Quick variable output
Shell Script Custom IFS + loop Full control over formatting Complex processing needed

Key Points

  • Always use double quotes when echoing variables to preserve whitespace and linebreaks

  • Command substitution removes trailing newlines by default in bash

  • IFS variable controls how the shell splits words and lines

  • Here-strings provide a clean way to pass variable content to commands

Conclusion

Preserving linebreaks when storing command output is essential for maintaining the original formatting. The double quotes method is the simplest and most widely used approach, while here-strings and custom shell scripts offer additional flexibility for specific scenarios.

Updated on: 2026-03-17T09:01:39+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements