Updated April 14, 2023
Introduction to Bash Script Variables
For people who have been into programming, variables are a concept that is widely used irrespective of the type of programming type or language. But in case this is your firsthand on understanding variables, we would try to give an analogous circumstance to get a feeling of variable and then get into the technical aspect of variable. In a big retail store, we have a temporary store where things might be stored for a temporary purpose, and when the items in the front end are over, the ones from a temporary store are loaded into front end. Similar to that, a variable is also a temporary store for storing a piece of information. These variables are pretty handy when it comes to managing and control actions in a programming scenario. In bash script itself, these variables go a long way in allowing developers to manage and control the flow or the outcome of an algorithm in the coding world! Not only this since they are so easily adaptable to circumstances, one can land up into grave trouble if the understanding is not proper in terms of working of variables. Hence, in this article, we will make sure that there is a proper understanding of even the minute things to help you get the best in just one article!
How do Bash Script Variables work?
Now let us answer the very basic question first, how do a variable work? To answer this, we would need to first understand the 2 actions that can be performed on a variable and they are:
- Variable value setting.
- Variable value reading.
We would slowly see the following actions in place below, but for now let us come back to understand how the variable work. With the 2 actions mentioned above, a variable itself can be set and assigned a value, which essentially means that the variable is now containing a value. Now this variable will be more useful when the value assigned to the variable can be used in any operation. This is where variable value reading comes to play!
Feature
Now let us look at some feature sets which is brought to the table by variables.
- Command-line arguments: As the name suggests, these are the arguments sent through the command line. Now the bash will try to interpret the arguments sent and assign each of them to a variable. They are referenced in a special way, i.e. using $<number> for example, $1 for the first argument, $9 for the 9th argument, and so on!
- $USER: This is again a reserved variable, which is assigned with a value of username of the user to run the script.
- $RANDOM: This is another reserved variable, which returns a new random variable every time it is referenced to.
- Usage of Quotes: In case a developer wants to assign a value, and this value has spaces in it, we would use quotes at the start and end of the variable to make it as a single value for assigning. The quotes can be single or double!
- Command Substitution: In some special cases we would like to execute a command on the fly and assign the result of the command to a variable. It is done easily in bash by just preceding the command with $ sign and putting the entire command within brackets!
Few pointers to be remembered while dealing with variables:
- When we read a variable for its value, we would use $ sign to reference that the name given is a variable whose value we are trying to access.
- While value is set for a variable, we don’t use the $
- As a standardized coding practice, camel-casing is the most preferred naming convention for variable names.
- There is no clear restriction on the placement of a variable.
Examples to Implement Bash Script Variables
From the above theoretical concepts we discovered some features which can be utilized effectively as a bash script argument and now in this section we will discuss an example where we would showcase the above features’ actual working right in front of your eyes giving you enough confidence to try them in real-life problem-solving.
Code:
#!/bin/bash
echo "**We would look at the arguments sent by command line:**"
echo "Argument number 1: $1"
echo "Argument number 2: $2"
echo " "
echo "The name of the user running the shell is: $USER"
echo "The first random number generated is: $RANDOM"
echo "The second random number generated is: $RANDOM"
var1="World of EduCBA"
echo " "
echo "The varriable: $var1 has spaces but still considered as single variable: No Error"
echo "Similar to the * command @ command also prints all the values passed in the script as: $@"
echo " "
echo "Understanding how command is substituted for a variable"
var2=$( ls | wc -l )
echo "We have $var2 entries in the current working directory!"
Output:
Conclusion
So now we can confidently say that we have in-depth covered the workings of bash script variables and this working architecture of variables is the single most important tool for bash script to help these scripts enjoy the flavor of success they currently enjoy! Some other topics are there as well like exporting of variables, but these are not widely used in the industry and hence have kept them for some other placeholder to talk about!.
Recommended Articles
We hope that this EDUCBA information on “Bash Script Variables” was beneficial to you. You can view EDUCBA’s recommended articles for more information.