Categories: TutorialsUbuntu

How to Store File Content in Shell Variable

Typically, Linux administrators and developers use shell variables to store strings and numbers. But sometimes you may need to store file content in shell variable. In this article, we will learn how to do this.

How to Store File Content in Shell Variable

Let us say you have the following text file data.txt.

text1
text2

We use cat function to output the file contents.

cat data.txt

We assign the output of above command to a shell variable test_var using the following command.

test_var = $(cat test.txt)

Generally, administrators use the following command to output the contents of a variable.

echo $variable

If we do this with the above variable, you will see the following output.

Sponsored
class="wp-block-preformatted"># echo $test_var text1 text2

You will see that the newline has been omitted in the above output.

To display the file contents exactly as they are, you need to enclose the shell variable within quotes, as shown below.

Sponsored
# echo "$test_var"
text1
text2

Now the file contents are displayed exactly as they are. This happens because when you issue a command in Linux shell, it uses IFS (internal field separator) to split the output after expansion using space/tab/newline, and any of these can be used to split the output into a list of words. So the shell reformats the output such that all words are separated by space and a newline is added only at the end of the output.

When we use quotes around our shell variable, it forces bash to treat the entire output of variable (that is, file content) within quotes, as a single string, and it is not split.

In this article, we have learnt how to store file content in shell variable.

Also read:

How to Sort Array of Objects by Multiple Objects
How to Get Position of Element in HTML
How to Get Duplicate Values in JS Array
How to Get Multiline Strings in JavaScript
How to Split String by Particular Character

The post How to Store File Content in Shell Variable appeared first on Fedingo.

Ubuntu Server Admin

Recent Posts

Canonical Releases Ubuntu 25.04 Plucky Puffin

The latest interim release of Ubuntu introduces “devpacks” for popular frameworks like Spring, along with…

2 days ago

Ubuntu 25.04 (Plucky Puffin) Released

Ubuntu 25.04, codenamed “Plucky Puffin”, is here. This release continues Ubuntu’s proud tradition of integrating…

3 days ago

Extended Security Maintenance for Ubuntu 20.04 (Focal Fossa) begins May 29, 2025

Ubuntu released its 20.04 (Focal Fossa) release 5 years ago, on March 23, 2020. As…

3 days ago

Ubuntu 20.04 LTS End Of Life – activate ESM to keep your fleet of devices secure and operational

Focal Fossa will reach the End of Standard Support in May 2025, also known as…

4 days ago

Ubuntu MATE 25.04 Release Notes

Ubuntu MATE 25.04 is ready to soar! 🪽 Celebrating our 10th anniversary as an official…

5 days ago

Ubuntu Weekly Newsletter Issue 887

Welcome to the Ubuntu Weekly Newsletter, Issue 887 for the week of April 6 –…

6 days ago