FUNDAMENTALS A Complete Guide for Beginners
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.
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
❶ At first, launch an Ubuntu terminal.
❷ Write the following command to open a file in Nano.
nano script1.sh
- 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”
#! /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
- 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 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.
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”
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 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 |
|
|
Method 02 |
|
|
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
Related Articles
- How to Echo Variables in Bash Script? [4 Practical Examples]
- How to Use String Variables in Bash Script? [4 Cases]
- How to Append String to Bash Variable? [2 Effective Ways]
- How to Check If Bash Variable Exists? [2 Effective Methods]
- How To Check if Bash Variable is Empty? [2 Easy Methods]
- How to List and Set Bash Environment Variables? [3 Methods]
- 2 Ways to Unset Environment Variables Using Bash Script
- 5 Methods to Check If Environment Variable is Set in Bash Script
- How to Set Bash $PATH Variable? [Easiest Configuration]
- 2 Cases to Execute Command Stored in Bash Variable
- How to Store Command Output to Bash Variable? [3 Examples]
- How to Write Bash Variable to File? [3 Effective Methods]
- Compare Variables in Bash Scripts [3 Practical Cases]
- Increment Variable Value in Bash Scripts [4+ Examples]
- Adding 1 to Bash Variable [3 Examples]
- Decrement Variable Value in Bash Scripts [4+ Examples]
- Addition of Bash Variable [4+ Examples]
- How to Subtract Two Bash Variables? [4+ Easy Approaches]
- How to Multiply Variable in Bash [6+ Practical Examples]
- Variable Substitution in Bash [Replace Character & Substring]
<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial
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.
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)
If you comment “echo “$var”” line and run the script in the terminal you see printed data from weekday.txt. Method 02 works correctly!
Yes, It works. Thanks for your observation and appreciation.