How to create and assign values to variables in Bash shell scripting?


A Variable is a character string to which we assign a value. The value could be a number, filename or any other type of data. Simply we can say a variable is a pointer to a particular value or data. By default Shell allows us to do create, modify, delete the variables, remember these variables are only a temporary one🙄 i.e when you close the session, by default these variables get deleted🤨. So if you want to make a variable and its value permanent i.e to make it as a system-wide you should export the variable or updated it in to the environmental variable.

What is Valid  and invalid variable names?

You should always remember a variable name must start with a letter or underscore, but not with a number.

For example, a valid variable name would look like this👇🏿👇🏿



3. _hello_


An invalid variable name look like this👇🏿👇🏿


If you use a hyphen(-) or u start a variable with numbers then those are considered to be as the invalid variable names.

Types of Variables

There are two types of variables exists in bash shell scripting

1.Local variable

2.Environmental variable

What is Local variable?

Local variables are the variable that is contained exclusively within the shell in which they were set or defined. i.e once you close the terminal or session the information will get erased from the memory.

What is Environmental variable?

E.v are the variables that are defined for the current shell and are inherited by any child shell or processes i.e it updates a value system-wide if you want to make a variable value permanent you should update it in an environmental variable.

Note: To export a value systemwide you will need to open the following file /root/.bashrc and update the variable and its values.

How to assign a value to a variable in bash shell script?

Assigning a value to a variable in bash shell script is quite easy🤠, use the following syntax to create a variable and assign a value to it.



Ex:1 Create a variable and assign a value to it

Let me create a variable with the name myvar and assign a value ABC to it.


How do I display the values from the variable?

Use the following syntax to display the values from a variable


#echo  $variablename

Ex:2 To display the value from the variable called “myvar”

#echo  $myvar

Sample output: ABC
Note: If you skip the $ sign then it would display  only the variable name “myvar”, the shell will not execute the values from the variable
Note: “echo” command which is used to read the values from the variable.


Ex:3 Let us create a shell script to display the total no.of lines in a file

Method: Static

#vi  / 


       echo "Display the total number of lines in a file"

       cat  /var/log/messages  |wc  -l 

      echo  "End of Script" 




Line:1  Define the interpreter(bash is the interpreter here)

Line:2 Prompt a message on the screen

Line:3 The command to display the no of lines in /var/log/messages file

Line:4 After displaying the output it prints end of script  message on the screen

Note: Since I have created the above script by using the static method, whenever I run this script it displays the same output.

               In our next article, I will be explaining about the “quotes” in bash shell scripting


If you found this article useful, Kindly Subscribe here 👉  Click this link to Subscribe

Never miss an article Do like my official  FB page 👉 Learn Linux in an easier way