How to Write Bash Variable to a File? [3 Effective Methods]

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.

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

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.

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

#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
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, 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
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 the following command:

./script1.sh

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

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

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
EXPLANATION

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

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

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
EXPLANATION

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 script3.sh Bash script has taken the value of some variables from the user and then written them to a file named biodata.txt.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
  • It is a straight-forward process to write a variable to a file.
  • You can not write a variable along with a string to a file.
Method 2
  • In this method, you can write a variable along with a string.
  • It is a comparatively long command.
Method 3
  • In this method, you can take input from the user and write the variable to a file.
  • You need to manually input all the information of the variable which seems time-consuming.

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

Where are bash variables stored?
These variables are set and configured in /etc/environment, /etc/profile, /etc/profile. d/, /etc/bash. bashrc files according to the requirement.
How do you write a variable in Bash?
You have to declare any variable name and assign its value as per the below syntax. variable_name=(“$variable_value”)
What is the use of set in Bash?
The set is a command that allows you to enable certain flags in your Bash script to make the script follow certain behaviors and characteristics.
What are the uses of variables in shell script?
Some common uses of variable in a shell script is storing data and information, taking input from users, and printing values that are stored. They are also used for storing data temporarily and storing the output of commands.

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

Leave a Comment