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, here-string, double quotes, and shell script.

Preserve Linebreaks When Storing Command Output to a Variable

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. It also has some different methods, which are as follows:

Double Quotes

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

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. After command substitution, you use the here-sting as follows:

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

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}" ((i++)) 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

Conclusion

Sometimes we need to get the actual output while doing command substitution. For example, if there are linebreaks in the file, they are not preserved at the time of command substitution.

Here we looked at preserving linebreaks when storing the command output to a variable through double quotes on variables, using shell scripts, and here-strings. With the help of the above information, you can preserve linebreaks at the time of command substitution in Linux.

Updated on: 13-Sep-2023

312 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements