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!
- 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.
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:
❸ 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
❹ 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
❻ Run the script by the following command:
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
Run the script by executing the following command:
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
Run the script by executing the following command
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.
Here I have listed some pros and cons of the methods mentioned above. I hope this will help you to choose your suitable method.
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.
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.