JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Reading a file into a variable in Bash

Author:JIYIK Last Updated:2025/03/23 Views:

Sometimes when dealing with files, we may need to place the text file in a variable in order to perform some necessary operations on the file. In Bash Script, this is a very easy task that does not require more than two lines of code.

This article will show how we can get a file in a variable. Also, we will see necessary examples and proper explanations to make the topic easier to understand.


The text file to save into a variable

Before we begin, assume we have a text file with the following content:

This is the first line.
This is the second line.
This is the third line.
This is the fourth line.

Now, we will use the following three methods one by one to get the file into a variable,


Reading a file into a variable using cat keyword in Bash

In our following method example, we will use the Bash's built-in keyword "cat." This keyword is mainly used to read any file.

The code for our example is as follows:

FileText= cat 1_Test.txt
echo "$FileText"

In the above example, we are simply reading a text file using cat 1_Test.txt and assigning the data to a variable called FileText and then printing the data stored in the variable.

After executing the above Bash script, you will get the output as follows:

This is the first line.
This is the second line.
This is the third line.
This is the fourth line.

Reading a file into a variable in Bash without using the cat keyword

This method will perform the same task but will not use the keyword cat. You can put a text file into a variable as shown in the following example.

FileText=$(<1_Test.txt)
echo "$FileText"

In the above example, we are simply $(<1_Test.txt)reading a text file using and assigning the data to a variable called FileText. After that, we print the data stored in the variable.

After executing the above Bash script, you will get the output as follows:

This is the first line.
This is the second line.
This is the third line.
This is the fourth line.

You can choose any of the above methods according to your preference.

请注意, all the codes used in this article are written in Bash. It will only work in Linux Shell environment.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Hosting Docker Internal in Linux

Publish Date:2025/03/23 Views:143 Category:OPERATING SYSTEM

Docker allows developers to efficiently build, test, and deploy applications by packaging them in standardized units called containers. When working with Docker containers, you may encounter scenarios where you need to connect a container t

Setting the working directory in Docker

Publish Date:2025/03/23 Views:198 Category:OPERATING SYSTEM

If present, the working directory of a process in Compute is a directory in a linked hierarchical file system that is dynamic for each process. In Docker, we can set our working directory by editing the Dockerfile and adding the key WORKDIR

How to get IP address in CentOS

Publish Date:2025/03/23 Views:108 Category:OPERATING SYSTEM

This short article is a brief introduction to CentOS followed by a brief discussion on how we can get the server IP address in CentOS using the Command Line Interface (CLI). This article will discuss some of the commands and their usage for

Updating YUM in Linux

Publish Date:2025/03/23 Views:148 Category:OPERATING SYSTEM

This article will teach us how to update YUM in Linux and how to install, update, remove, find and manage packages on a Linux system. We have also seen the difference yum update between and in Linux yum upgrade . yum update command in Linux

Installing Deb Files in Linux

Publish Date:2025/03/23 Views:93 Category:OPERATING SYSTEM

In this Linux article, we will learn how to install .deb (Debian Package) files on Linux systems. We will also see how to remove .deb files after installation. More importantly, we will learn different ways to install .deb files on Linux sy

lsof Command in Linux

Publish Date:2025/03/23 Views:100 Category:OPERATING SYSTEM

In this Linux article, we will learn about lsof command in Linux operating system. We will see how to use this command for different purposes in Linux. We use lsof the lsof command to verify the ports in use on the Linux operating system. U

ps aux command in Linux

Publish Date:2025/03/23 Views:57 Category:OPERATING SYSTEM

If you are using Linux and are looking for a tool that can monitor all the processes running on your system, then you should use the command ps aux. This command will show you an overview of all running processes. It is very useful for trou

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial