Introduction on Variables in shell scripting
Variables in Shell Scripting: The variables are of two types of user-defined variables and system-defined variables. A variable is nothing but a name representing a piece of memory in the system that stores a particular value, and that value no need to constant, which means it can change. Once we create a variable, we can access the variable, modify the variable, read the variable, perform operations on it, etc.
When declaring a variable, its name should be in capital letters. We can declare a variable as a variable name followed by assigning operator (=) and the value, which may be a character or string or a number or a special character. There should not be a space between the assignment operator and the variable name, and the corresponding value.
Why do we need Variables in Shell Scripting?
If we do not use variables in shell scripting, then we need to perform operations directly on the data like string, numbers, special character using its ASCII values, etc. If we don’t use variables, we can’t write programs that will work on a large amount of data; without variables, we can write programs that are useful for one time. If we want to use it again, we need to modify the shell script or program and then execute it like that we need to do it.
Suppose if you want to write a program to calculate the area of the rectangle with given height and width without variables, we need to edit the height and width values to enter user values every time we need to change and compile the program to execute it. So we need variables to write programs easily by storing data, and when computation happens, actual data will come, and operations will be performed on them. By using variables, we can write programs that can scale to a large extent using large data.
How to declare Variables in Shell Scripting?
There is no need to declare a variable in shell scripting, especially when we assign a value to any variable name; it will become a corresponding data type variable name. We can declare the variable in shell scripting as below:
variable_name=value, when we declare the variable, there is no space between the variable name, assignment operator, and its value.
If there is a space between them, then the shell will treat the variable name as a command to execute like below:
variable_name = value, here shell script will try to execute variable_name with space as a command with an argument. If we try to print a variable like an echoing world, her world is not a variable, but a string will display on the console. If we want to access the variable, then we need to access $ before the variable name.
the above sequence of commands will create a variable num with a value of 10 and then display that variable on the console.
echo ‘$num’ – it will throw an error because variable reference using single quotes is disabled in shell scripting as it causes interpretation of the special character $ literally.
How to initialize Variables in Shell Scripting?
In shell scripting, variables will be initialized when we assign some value to the variable name as below:
- var=” hello”: In this statement, a variable named var is defined and got initialized with a string hello. If we want to initialize a variable with a list of numbers or strings, then we need to give values separated by whitespaces enclosed in double-quotes as below:
- numbers=”1 2 3”: In this example, variable name numbers are assigned with a list of values 1 2 3 are separated by whitespace as we have seen in the example.
Var, this is a variable that is uninitialized, and we can’t use this variable to perform any operations in the shell script, as it behaves differently and causes several problems. By default, uninitialized variables will have the null value. So if we use this variable in any operations, we won’t get the desired result.
If we want to access the value of the variable in any operations, we need to give special character $ before the variable name so that it will replace with the actual value and operations will be done. If we didn’t use the $ before the variable name, then the script will behave differently.
How do Variables work?
In shell scripting, variables play a major role in storing the data and performing operations on the data using variable names as place holders for the data. We no need to define the variable’s data type before declaring the variable name as the shell can interpret the data type based on the values stored in the variable. The variable can store string, character, number, and special character. In shell scripting, variable names are case sensitive which means var and Var are different variable names. Suppose we have assigned a number to the variable and try to add a string to the variable now then it shell script throws an error saying non-numeric assignment error as below:
expr $str +1, this statement will throw an error saying non-numeric assignment because expr will accept only numeric variables. When we are declaring a variable name and assigning its value, there should not be any space between the variable name, assignment operator, and the value. When we want to access the value stored in the variable name, we should give $ before the variable name; then, we can get the value.
We can access the value in the variable name as below:
There are two types of variables in shell scripting, user-defined variables and system variables. The variable name is always will be in capital letters.
Examples of variables
Let us have a look at examples that will include both system variables and user-defined variables.
- BASH=/bin/bash – This is a system variable that will tell the LINUX environment’s shell name.
- PATH=/usr/bin:/sbin:/usr/sbin – This is a system variable that defines the path of the executable in the system where packages or libraries or needed files will be available when executing a program.
- HOME, USERNAME – These are also system variables that will tell use the home directory and the user name of the user who is using the system.
- VAR=10 – This is a user-defined variable that is storing the integer value.
- STR=” Hello” – This is a user-defined variable that is storing the string.
- Numbers=”1 2 3” – This is a user-defined variable that stores a list of values separated by spaces.
Finally, it is all about variables in shell scripting. So, far we have discussed what a variable in shell scripting is, why we need variables in shell scripting, how to declare a variable, how to initialize a variable, and how they will work in types of variables, and examples of shell scripting. I hope after reading this article, you will use variables in shell scripting in a better way than before.
This is a guide to Variables in Shell Scripting. Here we discuss the introduction, how to initialize Variables in Shell Scripting? how do Variables work along with the examples in Shell Scripting? You can also go through our other related articles to learn more –