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. 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.