Variables
Variables are the containers used to store data. In shell scripts, a variable can store any type of data, like - string, integer, real number, etc.
User-defined variables
The variables defined by the user are called user-defined variables. These variables are local variables.
You can create global user-defined variables also, we have covered this later in the article.
Naming Variables
These variables can be defined in lower-case as well as upper-case.
The variable name can be written using alphabets (a-z), numbers and underscore ‘_’
Note: The variable name cannot start with a number.
Example -
My_var
MY_VAR
var
newVariable
Variable1
Var_1
Defining Variables
To define a variable, write the variable name on the left side of the ‘=’ (equal to) sign and on the right side write the value of the variable.
Example -
my_var=ninja
Note: Do not give any space between the variable name, ‘=’ and value of the variable i.e my_var=ninja is valid but my_var = ninja is NOT valid and it will give an error.
These variables can hold only one value at a time, so in order to enter multiple-word sentences as the value of a variable, we will have to enclose it in “..” (double inverted commas) i.e. “Coding Ninjas”
Example -
MyVariable = “Coding Ninjas”
Environment Variables (Global Variables)
The Environment variables are accessible to all the shells.
They can be pre-defined by the operating system or can be user-defined.
To set a global variable, use the “export” command.
Example -
$ export my_var=ninja (my_var defined in the environment)
$ my_var2=ninja2 (my_var2 defined in shell)
$ bash (Start new shell)
$ echo $my_var
Output of above line - ninja (echoes value of my_var)
$ echo $my_var2 (empty)
Output of the above line will print nothing as my_var2 is a local variable and is not accessible when we start a new terminal.
In the above example, my_var is a global variable so when we change the shell, it is still accessible however, my_var2 is a local variable that is not accessible outside the shell in which it was declared.
NOTE: The environment variables are available for the shells that have been started after the variable was declared.
System Variables or Shell Variables
The system variables are defined by the operating system. Their values cannot be changed. These variables are required for the shell to operate properly.
We cannot change the name of the system variables. These variables are usually written in capital letters.
Some system variables are -
$BASH - It will output the name of the shell you are using.
$HOME - It will give the name of the home directory.
$PWD - It will give the address of the present working directory.
$HOSTNAME - It will give the name of the host.
Using Variables
To use a variable add ‘$’ (dollar sign) before the name of the variable.
Example -
my_name = “Ninja Coder”
echo $my_name
Frequently Asked Questions
How can we add multiple line comments in Shell scripts?
There are two methods to write multiple-line comments, you can start the comment with << followed by any word and finish the comment block with the same word that you used to start the comment block. Another method is to start with :’ and end the comment block with ‘.
What is the correct way to declare variables in Shell scripts?
The name of the variable goes to the left of the ‘=’ (equal to sign) and the value of the variable goes to the right. There should not be any space between the name of the variable, ‘=’ and the value of the variable. For Example, my_variable=ninja.
What is the difference between environment variables and shell variables?
Environment variables are global variables that can be accessed by all the shells that were formed AFTER the variable was declared.
Shell variables are system-defined variables with pre-defined names and values. They are local to the shell in which they are declared, i.e. they cannot be used outside of the shell in which they were declared.
Conclusion
Congratulations on finishing this article. In this article, we discussed how to add comments to the shell script and how to create, declare and use variables in shell scripts. We also discussed about different types of variables in shell scripts.
Do not stop learning. We recommend you read some of our articles -
🔥 Introduction to Linux
🔥 Linux Kernel
🔥 Linux Directories
🔥 Linux Commands list
Head to the Guided Path on the Coding Ninjas Studio and upskill in Data Structures and Algorithms, Competitive Programming, System Design, and many more courses.
If you want to Practice top Coding Problems, attempt mock tests, or read interview experiences, head to the Coding Ninjas Studio, our practice platform.
We wish you Good Luck!🎈 Please upvote our blog 🏆 and help other ninjas grow.