Introduction to Bash Export Variable
The following article provides an outline for Bash Export Variable. To start off, in case you are dabbed with other programming language, variables is something which will be quite familiar in your dictionary. For other who are not comfortable with other programming languages, a variable is a temporary placeholder to contain a number, a character or a string of characters. In bash specifically, one need not declare a variable, all we need to do is initialize or assign a value to the variable and in doing this, the variable will be created. Although these concepts are very easy to understand and use, a small deflection in understanding or their usage might make you land in a no mans land and coming out of that mess will be difficult. In other words, it is very easy and convincing to lead yourself into trouble if the understanding of the variables concept is no proper. Now before we see about what happens in the export command in bash, it is imperative to learn about how scripts work in bash.
One more thing to keep in mind is that it is absolutely not necessary to fully know the underlying principles of running a script in order to actually write a script and run it successfully, but it becomes absolutely necessary when one starts getting into complex scripts, where one script is dependent on calling another script withing itself and keeps the interaction that way for execution. In this, we have concepts of programs and processes. Program is nothing but a combination of data which allows you to run series of instructions for the CPU and then process the binary output to human recognizable format to be presented. On the other hand, a process is basically running an instance of the binary set of instructions, referred to as program. One program might have several processes running separately, independent of each other.
For example, 2 different terminals running the same copy command is an exact example. Now when we run a bash script by executing bash <filename>, obviously once all the required permissions are provided for it to execute, the command will internally create a process. This process is like an instance where all the variable sin the script itself will be existing. Now, if in another process, may be internally run by the same script we have run, we would like to access the variables within the parent script, it will be not possible at all due to permissions and restriction in-built for a bash for security reasons. Now, in order to use a variable which is invariably the one that may not be reproduced anywhere else, we can export the variable from the parent script to use it in any of the child scripts.
Syntax for the export of variable:
export <variable name>
In our example we will also look at a live interpretation of how export is a one-way process. When we call an export, only the variable value gets exported to any of the other child processes that might be initiated. What doesn’t happen is that any change in the child process doesn’t affect the parent process. This concept comes in very handy incase one follows the methodical coding standards where a script might be broken down into smaller modular scripts and some variables needs to be exported in between the parent and corresponding child processes. For example, a small task in another big task is to create today’s dated file names form an existing directory.
So, in the parent script, the list of files can be exported and, in the child process the task of prepending of today’s date is performed. One more point to keep in mind is that, by default all the variables which are defined in the scripts or are running within an instance are local. The -p option in export command helps in printing out all the variables which are getting exported. Also, -n option removes the export property from subsequent NAMEs. Also, by default environmental variables of parent script is exported to child processes.
Example of Bash Export Variable
Given below is the example mentioned:
echo "This is start of the parent process!"
var_exp="Initial Value Exp"
var_noexp="Initial Value Non"
echo "Variable to be exported is:: $var_exp"
echo " "
echo "Variable not to be exported is:: $var_noexp"
echo "Exported variable coming back after child process execution is:: $var_exp"
echo " "
echo "Non-Exported variable coming back after child process execution is:: $var_noexp"
echo "This is start of the child process!"
echo "Variable exported is:: $var_exp"
echo " "
echo "Variable not exported is:: $var_noexp"
echo "Changing variable exported to child process to 'Changed one Exp!' to see its effect in the parent variable"
echo "Changing variable not exported to child process to 'Changed one No_Exp!' to see its effect in the parent variable"
var_exp="Changed one Exp!"
var_noexp="Changed one No_Exp!"
echo "Variable exported is changed to:: $var_exp in child process"
echo " "
echo "Variable not exported is changed to:: $var_noexp in child process"
With the examples in this article, we are prominently in line with what exactly is followed in export of the variable and also how export is a one way process and no updating takes place in the variables which has been exported or any other local variable name used in the child process.
This is a guide to Bash Export Variable. Here we discuss the introduction to Bash Export Variable along with example respectively. You may also have a look at the following articles to learn more –