Shell ScriptingLearn about Variable in Linux Shell Scripting

Learn about Variable 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:

Firstname
LASTNAME
counter
_fullname
y1
grand_total
avg20

While the following are Invalid variable names:

first@name
1_count
Percentage%

* * * * * *

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 “=”
1

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:

echo $firstname
Ahmed

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

y=$x

Now, to print the value of the variable y:

echo $y
10

2

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.

unset variable_name

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

unset firstname
unset x
unset y

* * * * * *
Learn the Basics of C Programming Language

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.

env
DISPLAY=localhost:12.0
HISTSIZE=1000
HOME=/home/ahmed
HOSTNAME=centos
LANG=en_US.UTF-8
LOGNAME=ahmed
MAIL=/var/spool/mail/ahmed
PWD=/home/ahmed
SHELL=/bin/bash
SSH_CLIENT=192.168.1.5 1554 22
SSH_CONNECTION=192.168.1.5 1554 192.168.1.100 22
TERM=xterm
USER=ahmed

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.

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

$ cat show_script_name.sh
echo "Starting Execution of the $0 Script......"
date
echo "Hello, I am learning Linux Shell Scripting"
echo "=== End of Execution of the $0 Script ==="

Executing this script will display the following:

$ ./show_script_name.sh
Starting Execution of the ./show_script_name.sh Script......
Tue Aug 18 14:00:20 EST 2015
Hello, I am learning Linux Shell Scripting
=== End of Execution of the ./show_script_name.sh Script ===

* * * * * *

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

x=5
y=17
seq $x $y
5
6
7
8
9
10
11
12
13
14
15
16
17

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.

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

DATETIME=$(date)
echo "The Current Date and Time: "$DATETIME
The Current Date and Time: Tue Aug 18 18:27:26 EST 2015

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

ComputerName=`hostname`
CurrentUser=$(whoami)
echo "The Computer Name: "$ComputerName > Info.txt
echo "The Current User: "$CurrentUser >> Info.txt

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

cat Info.txt
The Computer Name: centos6
The Current User: ahmed

* * * * * *

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -