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.
There are two types of variables supported by Linux bash shells and these are:-
Variables Creation
User-defined variables can be created anywhere while working in terminal or writing a script by simply assigning a value to them.
Syntax:
Variable_name=variable_value
(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
‘read’
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.
Example:var1=local
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
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
Variables | Meaning |
---|---|
HOME | It 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 |
PATH | It 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 |
PS1 | It contains the shell prompt settings |
PS2 | It contains the secondary prompt settings that is displayed when you type an incomplete command |
LOGNAME | User’s login name is stored is stored in this environment variable |
SHELL | It stores the default shell of the user |
SHLVL | It contains the shell level that you are currently working in |
EDITOR | It is used to set the default editor settings on your Linux OS |
USER | It contains the by default user name |
UID | It 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.
/proc/<pid>/environ
• pid is the process ID.
• To view the process Id of your bash program run the following command
ps<Enter>
• 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