Introduction to Shell Script Set Variable
In order to understand about setting a variable it is of utmost importance for us to learn about variables first and know more about the elements involved in making a variable. The variable is taken as a string that is used for storing values. Although the variable itself is a string, it is not necessary that the value it can store is restricted to string only. It can be anything ranging from a string, number, filename, or any other object which shell is able to recognize.
Types of variables in Shell Scripting
Below are the three types of variables:
1. Local variables
These types of variables are present only within the running instance of the shell. In case there are child processes that are started by the shell script, the local variables might not be accessible to the child processes.
2. Environment variables
These types of variables are the ones that are accessible to any child process the shell script has run, unlike the local variables.
3. Shell variables
In certain cases, the shell might be required to set some variables in order to smooth the execution of the script and these variables are known as shell variables.
This article is about extending the environment variables, where we would use the set command to display a list of environment variables. In the below table we would look at those variables and their respective functionalities.
set -o <variable name>
Set Variables with Functionality
|allexport||This variable is used for marking those variables and functions which are to be exported to the environment. Until and unless we specify this variable, all variables are local and when this is turned on the all variables and functions will be transported to the subshell.|
|braceexpand||This variable performs brace expansion. This is a method used for generating strings at the command line. Mainly used for reusing a file path which is quite long to be written again in bash.|
|emacs||Using this variable one can use emacs styled editing interface in the command line.|
|errexit||This variable is to allow shell scripts to immediately exit if there is a non-zero status out of the script.|
|errtrace||Traps are cool techniques for implementing error handling when using bash. In this any errors which can be trapped is inherited by shell functions or substitutions in command.|
|functrace||Like the previous variable, this variable helps traps on DEBUG and RETURN to be inherited by shell functions or substitutions in command.|
|hashall||This variable helps in remembering the location of commands as and when they are looked up for execution.|
|histexpand||This enables shell scripts to use! style history substitution. One would have stumbled across the usage of ! is a sentence and get an error. This is because of the! style history substitution. If this annoys one, one can surely set off this variable.|
|ignoreeof||If you want to use Ctrl + D in windows to keep your session or script running, and not leave the shell, we would need to use this variable. In short, when the shell reads EOF, it will not exit. For example, if you set IGNOREEOF=18, one would have to press Ctrl+D 18 times to leave the shell.|
|history||This is to enable command history. You would have noticed these while pressing up button you can see the previous commands you have used.|
|monitor||This variable enables the shell script to have job control during execution.|
|noclobber||This variable enables bash to not overwrite an existing file using >, >& operators.|
|noexec||Using this variable one would just read the commands and not execute them. This is widely used for doing syntax checks in the code.|
|noglob||This variable option is used for disabling a file name generation or in other words, pathname expansion.|
|notify||This is a widely used variable, which is used when one wants to report the status of a terminated background process before the next prompt.|
|nounset||In the case of parameter expansion, any unset parameters are treated as an error when this variable is used in the set command.|
|onecmd||This variable as the name suggests executes one command and then exits.|
|physical||When this variable is used, symbolic links don’t work. For example, if one needs to change the directory, they can’t use the cd as a physical directory structure would be used.|
|pipefail||When this variable is used, the pipeline is returned with a value which is the last command to exit with a non-zero status. This is to understand the last point of error in the code.|
|posix||When used, the bash behavior is changed making default operation different from POSIX standard.|
|privileged||This allows security by running the shell script without inheriting it from the environment and hence the environment variables are not accessible.|
|verbose||This allows the input lines of the shell to be printed as they are read.|
|vi||This is to start a vi styled editing interface.|
|xtrace||This allows all the commands and their arguments to be printed as they get executed. Widely used for tracing back to an error in case many shell processes are run.|
Examples to Implement Shell Script Set Variable
Below are some examples mentioned:
echo "This example is to show some capabilities of set variables
echo "1. Now Every command will be printed as they get executed"
set -o verbose
echo "This is an echo statement: Assigned value to a variable as $variable"
Explanation: In the output you would see that all the command post point 1 gets printed as they are executed.
echo "This example is to show some capabilities of set variables"
echo "2. Now Every command will be read and not executed"
set -o noexec
echo "This is an echo statement: Assigned value to variable as $variable"
Explanation: In the above code we can see that the last echo statement is not executed, it is only read.
In conclusion, there are numerous variables available for set in a shell script and many of them are used by default. This variable gives more flexibility to the developer to use them in a script to achieve the desired result. Again, as we say every need in solving a problem statement is different and usage of these variables depends entirely on the problem statement. With that we sign off and meet you in another article of shell script.
This is a guide to Shell Script Set Variable. Here we discuss an introduction to Shell Script Set Variable, types, syntax, variables with functionality, and respective examples. You can also go through our other related articles to learn more –