Variables are the most important part of a Bash script. They can store important data, manipulate data, and do the necessary arithmetic operations. In such a condition, preserving the Bash variable is very crucial. And one of the most efficient practices to preserve variables is to write variables to file in a bash script. In this article, I will discuss the techniques of bash write variable to file. So without delaying, let’s start!
Key Takeaways
- Learning about the use of cat and printf commands to write variables to file.
- Knowing about the process of using here-string to write variables to file.
Free Downloads
3 Methods to Write Bash Variable to File
In this section, I have explored 3 methods to write Bash variable to File. The first one uses using echo or printf command, the second one uses the here-string, and the last one is about taking input from the user and then writing them to a file.
Method 01: Writing Variable to a File Using the “printf/echo” Command in Bash Script
You can easily write a variable to a file using the printf or the echo command in Bash script. The syntax for the printf command is:
printf "$variable_name" > file.txt
And the syntax for the echo command is:
echo $variable_name > file.txt
Here in this method, I will develop a Bash script that will utilize the echo command to write a variable to file. For more details, 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
#assigning value on var1
var1="Hello from var1"
#writing the var1 variable to the file1.txt file
echo $var1 > file1.txt
#printing the content of the file1.txt file on the terminal
cat file1.txt
#! /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, the var1=”Hello from var1″ command has assigned a value on the var1 variable. Afterwards, the echo $var1 > file1.txt command has written the value of the var1 variable on the file1.txt file. Finally, the cat file1.txt command has printed the contents of the file1.txt file on the terminal.
❹ Press CTRL+O and ENTER to save the file; CTRL+X to 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 the following command:
./script1.sh
The image shows that the Bash script has written the var1 variable to the file1.txt file and then printed the content of the file1.txt file on the terminal.
Method 02: Using the “here” String to Write Variable to File
Bash programmer can utilize the here-string to write a variable to file which is a technique to put string and variable value together. In this method, I have developed a Bash script using the here-string to store a variable in the file named file2.txt. To know more follow the below script.
Script (script2.sh) >
#!/bin/bash
#assigning a value on the var2 variable
var2="Hello from var2 -e \n"
#writing the var2 variable along with a string to file2.txt file
cat <<< "my variable is $var2" > file2.txt
#printing the whole content of the file2.txt file on the terminal
cat file2.txt
The var2=”Hello from var2 -e \n” line has assigned a value to the var2 variable. After that the cat <<< “my variable is $var2” > file2.txt line has written the var2 variable to the file2.txt. Finally, cat file2.txt line has printed the contents of the file2.txt file on the terminal.
Run the script by executing the following command:
./script2.sh
The image shows that the script3.sh has written the var2 variable to the file2.txt then the content of the file2.txt file has been printed on the terminal.
Method 03: Reading a Variable From the User Then Writing it to a File
Here, in this method, I will take the Name, Place of Birth, Date of Birth, and Occupation as input from the user. After that, I will write these variables to the biodata.txt file. Finally, I will print the content of the biodata.txt file on the terminal. To know more, follow the below script.
Script (script3.sh) >
#!/bin/bash
#take the Name, Place of Birth, Date of Birth, Occupation as input from user
echo “Please Enter your Name”
read name
echo “Please Enter your Place of Birth”
read place
echo “Please Enter your Date of Birth”
read date
echo “Please Enter your Occupation”
read occupation
#writing the Name, Place of Birth, Date of Birth, Occupation to the biodata.txt file
echo “$name” >> biodata.txt
echo “$place” >> biodata.txt
echo “$date” >> biodata.txt
echo “$occupation” >> biodata.txt
#printing the content of the biodata.txt file
cat biodata.txt
The echo “Please Enter your Name” read name like lines have printed a line and take Name, Place of Birth, Date of Birth, Occupation as input from the user. After that the echo “$name” >> biodata.txt lines have written the Name, Place of Birth, Date of Birth, and Occupation to the biodata.txt file. Finally, the cat biodata.txt line has printed the contents of the biodata.txt file on the terminal.
Run the script by executing the following command
./script3.sh
The image shows that the script3.sh Bash script has taken the value of some variables from the user and then written them to a file named biodata.txt.
Comparative Analysis of the Methods to Write Bash Variable to File
Here I have listed some pros and cons of the methods mentioned above. I hope this will help you to choose your suitable method.
Methods | Pros | Cons |
---|---|---|
Method 1 |
|
|
Method 2 |
|
|
Method 3 |
|
|
If you prefer straight forward process, you might choose method 1, if you want to write variables along with string, you might use method 2. And if you want to take input from the user, you might choose method 3.
Conclusion
In this article, I have tried to demonstrate to you some effective methods to write Bash variables to file using the echo or the printf command, here string command, and reading a variable from user input then writing it into a file. I believe that after going through this article, you will be productive enough to write a bash variable to file.
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 Read a File into Bash Variable? [2 Simple 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
FUNDAMENTALS A Complete Guide for Beginners