In addition to being a command line interpreter, Bash is also a very interesting programming language. It has so many different aspects that you can learn to control yourself in bash programming. In this article we familiarize ourselves with the method of writing a variable in a bash file using Ubuntu 20.04.

Method of writing a variable to a file in Ubuntu 20.04

To show how to write a variable to a file, we want to show a script in which the terminal asks the user for personal data such as name, age, date of birth, etc. and stores each of these entities in a separate variable. All these variables are then written into a text file. To do this, you must proceed as follows:

First we will write a bash program that can accept the user’s input and save it in a text file. To do this, we need to contact the file manager as shown in the figure below:

The file manager will send us directly to the home directory where we will create our base file to make it easily accessible. To create a bash file there, right-click the home directory and create a document using the Empty Document option. Now rename this document as you wish. In this case we called it VarFile.sh, as can be seen in the following figure:

Bash How to Write a Variable to a File – Linux Hint

Now open this file by double clicking and write the first required line, namely #!/bin/bash, to show that it is a bash-script.

Bash How to Write a Variable to a File – Linux Hint

Then enter the code or script in the newly created bash file, as shown in the image below. This script asks the user to supply his data one after the other. The user’s name, place of birth, date of birth, age and occupation are used as input data. An echo command in this script is used to display messages on the terminal, while a read command is used to store a user’s input data in the corresponding variables. Once the user has entered all the input data, this script saves the values of all these variables in a text file called BioData.txt. If an echo command is used, followed by a variable with the symbol >> and then the file name, an attempt is made to store the value of this variable in the specified file. Therefore, in principle, we try to store all values entered by the user in a single text file with their respective variables. In addition, we have put the variables in quotation marks, so that they are considered variables. If a variable is written between single quotes, it is treated as a string. In addition, if you want to approximate the value of a variable, you must enter $ for the variable, otherwise you will not be able to approximate its value.

Bash How to Write a Variable to a File – Linux Hint

After typing this script into the bash file, you have to save it and close it. After closing the file, start the terminal under Ubuntu 20.04 and type the following command to execute the just created bash script

Here you can replace VarFile with any name you give your bass file.

Bash How to Write a Variable to a File – Linux Hint

Once this script is completed, you will be prompted to enter your name, as shown in the following figure

Bash How to Write a Variable to a File – Linux Hint

After entering your name, you will be asked to indicate your place of birth.

Bash How to Write a Variable to a File – Linux Hint

Then the script will ask you to enter your date of birth.

Bash How to Write a Variable to a File – Linux Hint

After entering your date of birth, you will be asked to enter your age.

Bash How to Write a Variable to a File – Linux Hint

Finally, you are asked to mind your own business. This input stream matches the script we just created.

Bash How to Write a Variable to a File – Linux Hint

After entering all entries you will notice that a new text file called BioData.txt has been created in the homemap. You can check this by looking at the picture below:

Bash How to Write a Variable to a File – Linux Hint

You can now double-click on the content to check it or even view it via the terminal. Simply enter the following command into the terminal:

This command displays all the values stored in the specified file, i.e. the variables we wrote with the bash script in this text file.

Bash How to Write a Variable to a File – Linux Hint

In the figure below you can see that all our variables are written in the specified text file:

Bash How to Write a Variable to a File – Linux Hint

Conclusion

If you follow the steps described in this article, you can easily write as many variables as you want to a file, which means you can use the values of these variables in the future.

bash variable command not found,bash assign variable from command,variables in shell script examples,bash variable in single quotes,bash echo variable in string,mac bash script variables,bash variable substitution in quotes,bash parameter expansion cheat sheet,bash string substitution,bash nested parameter expansion,bash variable expansion in string,bash command substitution,bash commands cheat sheet,bash commands pdf,bash dot command,what is bash command,bash command options,bash command command,bash in 10 minutes,bash file extension,bash greg,github bash scripts,bash pipe,bash write variable to file without echo,bash use variable in command substitution,bash variables,bash declare variable,bash variable in string,bash variable substitution,how to pass variable in shell script,bash use variable in string

You May Also Like

Data Access Requests Under GDPR and CCPA

If your company is subject to data protection laws, such as the…

Barnes & Noble warns customers it has been hacked

The American bookstore giant Barnes & Noble contacts its customers by e-mail…

U.S. Bookstore giant Barnes & Noble hit by cyberattack

American book giant Barnes & Noble discovered a cyber attack and the…