Updated June 12, 2023
Introduction to 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 be 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 the operator (=) and the value, which may be a character or string or a, number or special character. There should not be a space between the assignment operator, the variable name, or the corresponding value.
Why do we need Variables in Shell Scripting?
If we do not use variables in shell scripting, we need to perform operations directly on the data like strings, numbers, special characters using 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 you want to write a program to calculate the area of the rectangle with a 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 quickly by storing data, and when computation happens, actual data will come, and operations will be performed on them. 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, we must 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 initialized with the 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 separated by whitespace, as seen in the example.
Var, this is an uninitialized variable, 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 a null value. So if we use this variable in any operations, we won’t get the desired result.
Suppose we want to access the value of the variable in any operation. In that case, we need to give a special character $ before the variable name to replace it with the actual value, and operations will be done. The script would behave differently if we didn’t use the $ before the variable name.
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 do not 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 strings, characters, numbers, and unique characters. 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 its 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 and system variables. The variable name is always will be in capital letters.
Examples of variables
Let us look at examples that will include both system and user-defined variables.
- BASH=/bin/bash – This system variable will tell the LINUX environment’s shell name.
- PATH=/usr/bin:/sbin:/usr/sbin – This system variable defines the executable’s path where packages or libraries or needed files will be available when executing a program.
- HOME, USERNAME – These are also system variables that will tell us the home directory and the user name of the user using the system.
- VAR=10 – This is a user-defined variable that stores the integer value.
- STR=” Hello” – This user-defined variable stores the string.
- Numbers=” 1 2 3″ – This user-defined variable 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. After reading this article, I hope you will use variables in shell scripting better than before.
We hope that this EDUCBA information on “Variables in Shell Scripting” was beneficial to you. You can view EDUCBA’s recommended articles for more information.