Cracking IT Interview
As name implies, something which changes its value. When we have to store any value for any calculation in script, we use variable. Consider it as a memory space with some name assigned to it. Also you can think it as a container which contains some value and we give some name to each container to recognize and use it.
Declaring a variable by data type is not required in Unix. Directly we give a name and assign the value to it by assignment operator(=). Remember, do not give any white space before or after '=' sign given.
NAME="Vikas K Gupta"
We keep the variable name in capital letters mostly by convention but there is no restriction over small case letters with variable names as well. Variable names contain alphabets (A to Z / a to z), numbers (0 to 9) or an underscore sign (_). Second thing to remember, Variable name can start with either alphabets or an underscore and not with numbers. Special symbols (other than underscore) are not permitted within a variable name.
Accessing variable value:
Just to access the variable value, give "$" just before its name.
Ex: echo "your name is $NAME"
Deleting variable value:
we can delete the value using "unset" command as:
Ex: unset NAME
But, we can't delete the values of variables which are declared as "readonly".
We can make a value constant with the variable by "readonly" keyword. We can't change the value further once it is declared as read only.
Ex: readonly NAME
Example Script: Swapping of numbers between 2 variables
#Comment: Entering the value for two variables and assigning them
echo "Enter the value for VAR1 = "
echo "Enter the value for VAR2 = "
#swapping values between two variables
echo "Value of third variable used in swapping = $VAR3"
echo "Value of third variable after swapping and unset = $VAR3"
#Printing the value of variables
echo "Value of VAR1 = $VAR1"
echo "Value of VAR2 = $VAR2"
echo "Value of VAR3 = $VAR3"
[Vikas ~]$ sh swapscript.sh
Enter the value for VAR1 =
Enter the value for VAR2 =
Value of third variable used in swapping = 50
Value of third variable after swapping and unset =
Value of VAR1 = 100
Value of VAR2 = 50
swapscript.sh: VAR3: is read only
These are the variables which already defined by the system and hold some default values. We also call it as system variables. All variable names are in uppercase letters by convention. Whenever we have to use these variables in our script to print some system values, just use "$" before the variable name as we are using in case of ordinary variables.
To list out environmental variables, use "env" or "printenv" command at the prompt:
[Vikas ~]$ env
SSH_CONNECTION=18.104.22.168 5672 22.214.171.124 22
SSH_CLIENT=126.96.36.199 5672 22
To know value of some specific environmental variables, print with "echo" at the prompt as:
Another way to list out your environment variable and set the environment:
[Vikas ~]$ cd ~
[Vikas ~]$ ls -la .profile
-rw-r--r-- 1 pinku guest 118 Jun 4 2017 .profile
[Vikas ~]$ vi .profile
PS1="$USER |`date` >>"
After changing the PS1 value when I am opening the new session window, prompt is appearing as below:
Vikas |Sat Jul 7 11:02:51 EDT 2018 >>
To change your shell prompt temporarily for one session, simply assign the required value ("$" below) to PS1 as:
email@example.com|Sun Jul 8 03:29:45 EDT 2018 >>PS1=$
Set up environment / Changing the value of environmental variable:
Standard way of doing this is to assign the value to variable and export it like:
(sh and bash shell)
Special variable which can hold multiple values. Actually, it is used group multiple values in contiguous memory location with one name assigned to it. We use index numbers for each values stored in array which starts from zero. There is no limit of values which can be stored in array.
ARRAY_VAR=(value1 value2 value3 …..)
Values must be separated with white spaces enclosed within parentheses() at the time of defining array.