Variables play a very interesting role in any programming language when it comes to processing and storing some data in memory whether temporarily or permanently. Variables create their unique storage location in RAM and these locations are assigned a unique number called memory address.

Types of Variables in Linux Shell

There are two types of variables supported by Linux bash shells and these are:-

User-Defined Variables

Variables Creation

User-defined variables can be created anywhere while working in terminal or writing a script by simply assigning a value to them.

(Don’t use space on either side of ‘=’ sign)
Example 1:	 a=5
Example 2: 	B=57
Example 3:	My_name=Bhavna
Example 4:	My_Message=”Hello Programmers..!!”


Referring to Variables

Variables can be referred (or the value stored in any variable can be referred) by just prefixing ‘$’ character to the variable name.

Example 1: 	My_name=Bhavna
		echo $My_name or echo ${My_name}

Output: Bhavna

Example 2: 	var=3
		echo ${var}rd

Output: 3rd


Reading a Value from User during runtime

You can specify a value to the variable during runtime from command using command

Example 1:	read My_name


When you run this command the interpreter will wait for your input and whatever value you will type via your keyboard till you press Enter will be assigned to the variable.

Example 2: read –p “Enter your name” My_name

read -p “text” variable will print the text without a newline and wait for value from user during runtime.

read –p “Enter value to variables=” variable1 variable2 …. variableN We can input value to many variables in this way.

Scope of Variables- Local and Global Variables

Local Variables-

available for use and visible only in the shell where the variable is created. By default all user defined variables are local. Try the following example.

		echo $var1		// Output: local
		echo $SHLVL		//Output: 1
		bash <Enter>		//concept of nested shell
		echo $SHLVL		//Output: 2 //indicating nested  shell
		echo $var1		//no output will appear


Global Variables-

visible in the creator shell as well as all the sub shells. Created using keyword export.

Example: var1=global
		export var1


On export, the variable is exported to all the nested shells

System Variables (Environment Variables)

Existence of System Variables

• The kernel stores the list of environment variables and their values for each process.
• Kernel inherits them to child processes.
• These variables are not stored in any file.

List of Environment Variables

HOMEIt contains location of the home directory of the user
cd<Enter> command will always take the user to home whose value is stored in this variable
PATHIt contains the list of path name of directories separated with colon ‘:’ These directories are searched for an executable program. The directories are searched in the same order as specified in the variable
PS1It contains the shell prompt settings
PS2It contains the secondary prompt settings that is displayed when you type an incomplete command
LOGNAMEUser’s login name is stored is stored in this environment variable
SHELLIt stores the default shell of the user
SHLVLIt contains the shell level that you are currently working in
EDITORIt is used to set the default editor settings on your Linux OS
USERIt contains the by default user name
UIDIt contains the default user’s ID

Commands to List all the Environment variables on your machine

• env<Enter>
• printenv<Enter>

/* PS: There is a virtual file which contains all the environment variables. Kernel makes them visible using this virtual file.
•	pid is the process ID.
•	To view the process Id of your bash program run the following command
•	For Example the PID for bash process comes out to be 8914.
     Now run the following command
		cat /proc/8914/environ
((All the variables will be listed without any delimiting character. Or in other words the variables in this file are delimited by a \0 (binary zero) character.)) */


Referring to System Variables

These variables can be referred (similarly as User defined variables) by just prefixing ‘$’ character to the variable name.

Examples: 	echo $HOME
		echo $PATH
		echo $SHLVL