Introduction to Bash Set Variables
So, for the first instance, let us start with understanding the different types of variables which are available in bash. Out of them, one corresponds to a family which includes set command variables. But before we even get into this, a variable is a storage element, which stores value when assigned. Though the variable name is a string itself, it is not at all necessary that the value it can store should also be a string. Even an integer, any decimal number, or even object can be stored in variables.
Types of Bash Set Variables
There are 3 different forms of variables which come in:
- Local Variable: When a bash process is created, if a variable’s presence is only restricted to the process, and not to any child process started by the bash, is termed as a local variable.
- Environmental Variables: In order for the successful execution of even a bash process, there must be some variables set for the environment we are working on so that the child process can access those variables during execution, and need to necessarily initialize the variable at every individual child process.
- Variables for Bash: In order for smooth execution of the bash, some variables might be required to be set and hence these are under the other type of variables.
Bash Set Variables & Description
Now, going ahead, we would expand the environmental variable while talking about set command and also talk about the different variables which are used for their respective and designated task. But before that let us look at the syntax, which is rather a simple one to remember and follow.
set -o <variable name>
The option mentioned in <> brackets signifies the different options we will be listed in the below table:
|Set variables||Description of where to use|
|allexport||In order to build functionality, that we need to export variables into the environment so that any of the child processes can use it, we would need the help of allexport to import all the variables. In case this option is not enabled, the variables will no longer be a part of the environment variable and hence restrict itself to only the bash the process is running on.|
|braceexpand||As the name suggests, it is used for brace expansion. Brace expansion is nothing but a fancy name for expanding string at execution. For example, after performing brace expansion one can reuse a file path instead of writing it again and again.|
|emacs||This variable is to enable emacs style interface for editing in the command line.|
|errexit||In case of an unlikely event that there is a non-zero status from scripts run by the bash, the bash will automatically error out in that instance.|
|errtrace||In case you have worked with another programming language, you would have the chance to look at the list of the trace when an error occurs. This trace enables one to exactly pinpoint the area of the problem.|
|hashall||This variable is widely used for remembering locations of the commands when they are looked up for execution. This helps in keeping these in one place for faster execution.|
|ignoreeof||Again, in this variable, the naming explains everything, that it ignores EOF, which means that if one wants to use Ctrl + D and still keep the bash working then use of ignoreeof is mandatory.|
|history||The command history is enabled and helps you to use up arrow to see the previous commands that has been used.|
|monitor||This variable enables the user to have job control during execution.|
|noexec||This is again one of the widely used variables in bash and enables you to not execute the commands in bash but rather read them to look for any active syntax errors. This is to help one to not waste execution time if the bash script has an error.|
|notify||If one wants to generate a report of the status of the background process, this is the variable you would be looking for to execute.|
|verbose||When this variable is turned on, the input lines get printed as they get read. This is for debugging purposes, in case one needs to know at what line there is an error.|
|vi||The most widely used “vi styled editor” is enabled using this variable.|
|xtrace||As the name suggests, this is for tracing the error back to its source and this is achieved by printing out the commands and their arguments along the way.|
Examples to Implement Bash Set Variables
Below are the examples of Bash Set Variables:
echo "Bash to have practical experience on set variables"
echo "Example to show that the each command is printed as it gets executed"
set -o verbose
echo "This echo command will get printed along with execution of the echo command and print variable value as $variable"
echo "The sum of numbers entered is $var_2"
echo " "
set -o noexec
echo "From now on nothing will get executed but only read"
echo "Only echo statement will get printed and not executed. You won't see the addition of the 2 numbers you have put"
echo "The sum of numbers entered is $var_2"
With this, we have come to an end of the article where in n we have gone through the most widely used variables in bash. These variables not only ease your working on bash by giving you ample options to debug, trace, syntax checks etc. but also put a formal process in place for programmers to control the flow of the code! In the end, we will sign off from this article till we meet in another part of the series of learning bash and shell the EduCBA way!
This is a guide to Bash Set Variables. Here we discuss the Introduction to Bash Set Variables and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –
- How to Declare Variables in Shell Scripting?
- How Does While loop Works in Shell Scripting?
- Introduction to Loops in Shell Scripting
- Top 5 Uses of Shell Scripting