The Bash variables are quite different from other programming language variables. The variables in bash do not require declaration, simply use the variable name to specify the data of the variable. The user-defined variables in the bash shell are considered the local variables. It implies that the variables of the shell are not passed down to the shell’s child processes. The variables must be exported by the user before they can be utilized by child processes. The “Export” command of bash is used to export the given variables to an environment where all child processes are running inside the shell. The export command is also referred to as the environment variable.
Environmental variables can be exported to child shells by being labeled with the export command. The export command enables us to notify the active session of any modifications made to the exported variable. The export command takes two arguments where the first argument is the different flags of the export command and the second argument is the variable name which is to be set for exporting in the subshell.
Example # 1: Usage of the Export Command in Bash.
Here, we have simply used the export command in our bash shell which displayed the following environment variables that are exported in our Linux system.
The export command of bash has some flags which provide different functionalities. The following bash shell is deployed with the export command that uses the flag “-p”. The “-p” flag is usually used to list all exported variables of the current shell. When we enter the command “export -p” on the bash shell, the list of all shell exported names are returned as follows:
All new processes are passed to the system environment variables, as seen in the above image. We can also get rid of the environment variables using another flag which is “-n”. The following command of export is set with the flag “-n” to unset these environment variables. The output displayed that the “export -p” command is undone with the command “export -n”, the variable is limited to the running shell session.
Example # 2: Usage of the Export Command in Bash to Export Variables.
The usage of the export command in the bash shell is explained in the aforementioned section. Now, we are using the export command to export the bash shell variable. The export variable command enables all the commands that are executed in the shell to access the variable. Here, we have used the export command with the variable name “Msg”. The export variable “Msg” is initialized with the string value. Then, we used the echo command that is passed with the “$Msg” variable. The echo command displayed the string value stored in the variable “$Msg”.
The environmental variable “Msg” created by using the export command can also be deleted. The unset command is used in the bash shell which is specified with the environment variable “Msg” to remove the value stored inside it. When we echoed the variable “Msg”, it returned the empty output as it was removed by the unset command.
Example # 3: Usage of the Export Command in Bash for Exporting Functions or Variables.
The export variable in the bash shell is demonstrated with the running command. We can also use the export command of the bash shell to export the bash function. In the following shell, we have first defined the function name “func()” where we have set the echo command to print the statement “Bash shell script”. Then, on the next line of the shell, we used the export command with the option “-f”. The “-f” option notifies the export command that this is the function. Otherwise, if we use the function without the “-f” option with the export command then the function will be considered a variable. The command “export -f” is given with the function “func()” below. After that, we executed the bash command and then the next line was provided with the name of the function “func” which returned the statement inside it.
export -f func
Example # 4: Usage of the Export Command in Bash to Export the Variable with the Value.
The export command also enables us to assign a value before exporting the variable. The command is given in the following bash shell where we first declare the variable “x” and provide the numerical value “10” against it. After that, we employed the export command and passed the variable “x” to it. Then, for printing the value of the environment variable “x” in the current shell, we called the printenv method. The printenv command of Linux returned the value “10” of the export variable “x”.
Example # 5: Usage of the Export Command in Bash by Using the Function.
Now, we are creating a separate bash file to acquire the expected outcomes from the export command. In the following bash command, we have first invoked the echo keyword which has the export command enclosed with the double quotes. The export command is utilized to set the environment variable “Str” and the “Str” variable is defined with the string statement ‘My new statement’ wrapped in the single quotes. After this, we used the right-angle symbol “>” and specified the file name “bashScript.sh”. Next, we used the bash “source” command which takes the file name “bashScript.sh” to import the export variable into the bash shell without generating a subshell. When we executed the source command, it displayed the export variable “Str” string value in the bash shell which is shown in the following bash shell screen.
The built-in export command of the bash shell is intended to export the environment variables to the subshell. We have illustrated the export command with a few examples. Additionally, we described the export command associated options “-p”, “-n” and “-f”. The variable set with the export command can be reused and acquired with any current shell of bash. We have also given an export variable which is accessed through the bash script.