In Linux, users can define the local variables in Bash by default. In this case, a user must export the variables for the child processes. That’s why Linux supports the export commands that update the current session per the exported variable’s updates.
Nevertheless, many users do not understand how to use the export command to the .bashrc file. In this tutorial, we will explain a complete method to export the variables in .bashrc and use them in scripts.
How to Export Variables in .Bashrc
Let’s start with the simple example of exporting a variable from a shell to the .bashrc file. First, we create a .bashrc file through the following command:
chmod +x file.bashrc
Now, create a variable and then export it in all shells. For example, set the value of the variable named example:
After that, export this variable through the following command:
You can now enter the new shell. Then, check the exported variable through the following commands:
Now, enter the following details in the file.bashrc:
Once you are done, save the file and execute it in the terminal:
The file.bashrc script exports the value from the example variable, as shown in the previous image.
Export Variables in .Bashrc and Use Them in Scripts
First, set the value of the test as the variable. Then, export it in all sessions:
Now, execute the script in the terminal to print the variable:
If you want to use this exported variable in other scripts, create a script first and then enter the following details:
Finally, run the script. The system will print the exported variable in the terminal:
The export command is not limited to the scripts related tasks. It also includes various options. For instance, you can use the -p flag to display the list of variables:
This is how you can easily export the variables in .bashrc and use them in the scripts. The export command is easy to use and can help you to export the variable value from the current session to all. Exporting the variables is important because the variable value is only available for the current session. You can use these exported values in different scripts.