Learn about Variable in Linux Shell Scripting

variables in linux shell scripting

Change is the essence of life; be willing to surrender what you are for what you could become.” Reinhold Niebuhr

Variables are named memory locations which are used to store values while computer programs are running. When a variable is declared (defined), a portion of memory (segment) is allocated for this variable. From this point on, this memory segment is reserved for the program. It can be written to, read, updated as needed in the logic of the program. When the program execution is complete, that memory segment is no longer needed, so it is de-allocated and marked as free for use, so that the operating system can allocate it to another process that needs memory.

Variable Names
A variable name is an identifier that should be unique to represent the specific memory location it points to. For an identifier to be a valid Linux Shell variable name, it must:

1) Start with a letter or underscore “_”
2) Not contain any characters other than: letters (a-z and A-Z), numbers (0-9), and underscore.

Following these two conditions, the following are valid variable names:

While the following are Invalid variable names:

* * * * * *

Using Variables
Variable are defined to be used. First, a variable is defined, and assigned a value. Next (somewhere in the program), the initial value stored in the variable is read and used in some data processing tasks or decision making. Later on, the value of variable may be updated and set to a new value. Finally, the variable may be read again for printing to the user.

Declaring and Initializing Variables
One step does both operations: declaration and initialization of a variable. a value is assigned to a variable using the normal assignment operator “=”

Reading Values of Variables
To read the value stored in a variable, we need to precede the variable name by the dollar sign character. So, to print the value of the variable firstname, use the following statement:

Similarly, to assign the value in the variable x to another variable y, use the following statement:

Now, to print the value of the variable y:

Destroying Variables
When a variable is no longer needed, it is wise to delete it, so that the memory segment reserved to it could be de-allocated, and be free for use by other processes. Variables are deleted using the command unset.

For example, to unset the three variables we have just defined:

* * * * * *

Environment Variables
Environment variables are collection of variables whose values are set, maintained, and updated by the Linux Shell. Usually, the names of the environment variables are in capital letters. We have already encountered an example of this type in the previous article, when talking about “Executing Scripts as Linux Executable Commands”; The PATH variable.

Environment variables are read and set the same way as normal variables. To get the list of all environment variables with their current values, enter the command env.

Special Variables
Linux Shell also uses another type of variables called Special Variables. These variables are reserved by the shell to be used for specific purposes.

One of these variables is the one that tracks the exit status of the last executed command $?. We have already talked about this variable earlier in this series. Another useful variable is the $0. This special variable contains the file name of the running script.

The following script is a modified version of our first script.

Executing this script will display the following:

* * * * * *

The following statements will define two variables x and y. Then the seq command will be used to print all numbers between x and y.

Assigning Command Output to a Variable
Variables can be used to store outputs from executed commands. This is useful when we need such outputs for further processing.

The following lines store the current date and time in as a string value in the variable DATETIME. Then the value of the variable is printed to the screen.

The following code gathers info about the machine name and the current user, and stores this data in variables. The stored data is then written to a file.

The file Info.txt should contain the data written to it:

* * * * * *

In this article, we have talked about variables. Variables are named memory segments, used to store data values inside the computer programs. A variable is first declared and initialized, then the value stored in it can be accessed for reading and writing as the program logic dictates. The great importance of variables may not be obvious now. Soon, we will talk about operators. At this moment, you will start to feel this importance. When it comes to decision making and loops, you will realize how vital variables are.

So, until this comes, study well!!!


Please enter your comment!
Please enter your name here