How to Read a File Into Bash Variable? [2 Simple Methods]

Reading a file into a variable carries an important potential in the Linux ecosystem due to its pivotal role in data processing, automation, and streamlined scripting. It enables users to execute frequently used bash programs on Bash script without mentioning it repeatedly. Reading files into variables bridges the gap between raw data and script execution, unlocking the potential for efficient data handling. In this article, I will explore ways to read a file into bash variable.

Key Takeaways

  • Using the cat command to read a file into the Bash variable.
  • Learning the process of using the input redirection operator to read a file into the Bash variable.

Free Downloads

2 Methods to Read Files Into Bash Variable

You can easily read a file into the bash variable. Here, I have a text file named weekday.txt.

Text file (weekday.txt) >

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Now I will show you two methods to readthis file into a Bash variable. The first method uses the cat command and the second method uses the input redirection operator.

You can read our Comparative Analysis of Methods to distinguish between these two methods and pick the best one for your needs.

Method 01: Using the “cat” Command to Read a File Into Bash Variable

You can easily read a file into a Bash variable using the cat command. Here, I will develop a bash script that will read the weekday.txt file into a variable named var utilizing the cat command. To know more follow the below procedures

Steps to Follow >

❶ At first, launch an Ubuntu terminal.

❷ Write the following command to open a file in Nano.

nano script1.sh
EXPLANATION
  • nano: Opens the nano text editor.
  • script1.sh: Bash script name.

❸ Copy the script mentioned below:

#!/bin/bash

#reading the value of weekday.txt file into the var variable
var=$(cat weekday.txt)

#printing the value of the var variable
echo “$var”
EXPLANATION

#! /bin/bash ‘#!’, is called shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash. After that in the var=$(cat weekday.txt), cat command has read the value of the weekday.txt file into the var bash variable utilizing command substitution. Then in the echo “$var” line, the echo command has printed the value of the var variable on the terminal.

❹ Press CTRL+O and ENTER to save the file; CTRL+X exit.

❺ Use the following command to make the file executable:

chmod u+x script1.sh
EXPLANATION
  • chmod: Changes the permissions of files and directories.
  • +x: Argument with the chmod command to add the executable permission.
  • script1.sh: File that you want to make executable.

❻ Run the script by executing the following command:

./script1.sh

The value of the weekday.txt file has been read into the var variable using the cat command then the value of the var variable has been printed on the terminal.The image shows that the value of the weekday.txt file has been read into the var variable using the cat command then the value of the var variable has been printed on the terminal.

Method 02: Using the Input Redirection Operator to Read File Into Bash Variable

You can easily read a file into a Bash variable using the input redirection operator. Here, I will develop a bash script that will read the weekday.txt file into a variable named var utilizing the input redirection operator. To know more follow the below script.

You can follow the steps mentioned in method 01 to know how to write, save and make the bash script executable.

Script (script2.sh) >

#!/bin/bash

#reading the value of weekday.txt file into the var variable
var=$(<weekday.txt)

#printing the value of the var variable
echo “$var”
EXPLANATION

The var=$(<weekday.txt) portion means that the input redirection operator has read the value of the weekday.txt file into the var bash variable. Then in the echo “$var” the echo command has printed the value of the var variable on the terminal.

Run the script by executing the following command

./script2.sh

The value of the weekday.txt file has been read into the var variable using the input redirection operator then the value of the var variable has been printed on the terminal.The image shows that the value of the weekday.txt file has been read into the var variable using the input redirection operator then the value of the var variable has been printed on the terminal.

Comparative Analysis of the Two Methods

Here I am going to show you a comparative analysis of the two methods of reading files into variables.

Methods Pros Cons
Method 01
  • Simple and straightforward syntax.
  • Suitable for reading multiple files and concatenating their contents.
  • Inefficient for large files because ‘cat’ reads the entire file into memory at once.
Method 02
  • Directly reads the file’s contents into the variable without invoking an external command like ‘cat’.
  • More efficient for large files as it avoids reading the entire file into memory at once.
  • Can only read a single file.

If you prefer a straightforward process or you need to read multiple files, you might choose method 01. But if you want to read a large file, you should choose method 02.

Conclusion

In conclusion, acquiring the skill to read files into variables is paramount for effective Linux use and programming. This article has comprehensively explored the two methods to read files into variables in bash scripts. I hope, after reading this article you have become sufficiently skillful to do this type of task.

People Also Ask

How to read the value from the file in Bash?
You can use the cat command or input redirection operator to read values from files in Bash. For this execute var= cat weekday.txt or var=$(<weekday.txt) command.
How do you read input into a variable in Bash?
You can use the built-in Bash command called read. It takes input from the user and assigns it to the variable.
How to check the variable value in the Bash script?
You can use the -v variable_name or -z ${variable_name} option as an expression with the combination of the “if” conditional command to confirm whether a variable is set or not in Bash scripting.
Is bash variable a number?
Bash variable is mainly character strings, but depending on the context, it permits arithmetic operation and comparisons of variables.

Related Articles


<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

4 thoughts on “How to Read a File Into Bash Variable? [2 Simple Methods]”

  1. Method 01 not working for me. I think for others users also not working. The “var= cat weekday.txt”, read data from weekday.txt file and print to the terminal. The echo “$var” print empty line because the variable “var” is not set.

    Reply
    • This bash script works fine from my end to read the file. However, if you want to modify this variable definition, you can utilize the command substitution by following this command: var=$(cat weekday.txt)

      Reply

Leave a Comment