Shell Scripting | Topic 'Variables'


 In previous post we had created our first real world script that show up some system details. This is simple as it is. But the most complicated scripts are not easy to write down as previous one. The one technique which is used to reduce the complexity of any program known as variables. If you have computer background or learning programming then you must know what variables are. If you already know what variables are then you can skip otherwise continue.

Variable Definition: A variable is a string of characters which can stores the data in it. It can be temporary or permanent.

Type of Variables In Bash There are basically three types of variables in shell scripting.
1) Local Variables: A variable which is defined by user itself and only used in a user environment(not affected to other users and system) is called local variables. Example : NAME="IT GUY AKAY"

2) Environment Variables: A variable which is defined in system & gloablly access by any user or application is called Environment variable. You can see the Environment Variables by executing env command or printenv command  Example : echo $SHELL
3) Shell Variables: A variable which comes under the shell itself ( like ksh, tsh, cshell , bash) are called shell variables. Example :  $BASHPID (in bash) , rmstar (in tcsh)

Actually variable is a pointer which points to is data value it has stored.You can store any type of data(integer,strings, any command etc) inside a variable. Generally you don't have to define data type bash scripting while declaring or assigning a value to any variable.But there are some basic rules to define a variable in bash shell scripting.
Below are some points to remember when you create a variable in bash shell scripting.
Naming :A variable can contain A-Z & a-z alphabets , 0-9 numbers , an underscore "_" characters in it. There are some example of creating a variable.
Correct Variables Examples
1) Variable_1="My Name"
Creating variable with upper and lower case alphabets , underscore and numeric. variable store the data My Name with or without quote ""  sign. 
2) Var1="123"
Creating variable with alphanumeric string
 
3) _name=itguyakay
creating variable starts with underscore & only lower case alphabets.

4) List=`ls -l`   or    List=$(ls -l)
Simple variable which holds the output of Linux command ls -l in List variable . For this you have to use tick (``) character around the command so it can assigned properly. You can also use the second method which is more visible and recommended
Wrong Variable Examples:
1) 1MyVar="MyName"
A variable will never be starts with a numeric character.
2) 1234="Name" 
A variable never be created with only numeric characters
3) !name@="itguyakay"
A variable will never be created with any other special characters except underscore(_)
Assigning Value : If you notice , when creating a variable and assigning a value/data to that variable we use equal (=) sign . when you assign a value to your variable make sure there is no gap between variable and assigned data
 
Correct Method : login_user=`whoami`
Notice there is no gap between our variable and its assigned value 

Wrong Method : login_user=  `whoami`
Notice there is a gap between our variable and its assigned value 

Use & Execution : After you successfully declare/create a variable , you need to use that variable in your scripts. For this we use dollar ($) sign to execute our variable. Below are some example:
Examples: path="/home/username/Desktop"
$ cp example.txt $path  
In above example, we have created a variable named path then we use that variable in our copy command. (see below pic)
bash variables

But sometimes you need to use variable with some specific functions or  special characters. see the below example.
$ user=`whoami`
cp example.txt /home/${user}/Desktop
Here we are using user variables in our copy command. Notice while using variable ,to prevent any confusion and make more clear & visible , we use curly brackets {} around our variable. This helps make us to find our variable in any script. Also this is useful when we create an array variable. (see below example)
$ array=(10 20 30)
$ echo ${array[0]}
  output:- 10
$ echo ${array[1]}
output:- 20 
Note: when we define an array variable we use brackets () & inside brackets put data values by giving them single space. (see below pic)

Assigning Variable Inside Variable : Variable are basically helpful when we have to fetch a data repetitive time, but what if we need fetch multiple data values multiple times and these data are interconnected. This is helpful when we create a complex scripts and mostly used in bash shell scripting See below is the example to understand and use of variable inside variable:
name=$(whoami)
user_info="System is login as ${name}"
cp example.txt /home/${name}/Desktop
echo $user_info
In above example, we have created two variables. name variable print the username of current login user and user_info variable will print the information about who is currently login(just an example here). Note that second variable is holding the first variable inside it. So whenever you use variable user_info  in side any command (e.g echo) it will also fetch the data of name variable.(see below output picture)
So these are some basic rules to create  variables in Bash Shell Scripting . No doubt ! there are more ways and rules to define a variable in bash scripting . But for the sake of this tutorial, we are just focused on basic understanding & learning of bash shell scripting. In next post, we will use variables in our scripts and learn how they work.

Hope You Like This Post..😉 Please Share This Post With Your Friends..😀
variables in shell scripting

Previous                                                                                                                                          Next

Post a Comment

0 Comments