- Unix Commands Reference
- Unix Commands - Home
export Command in Linux
export is a Linux command that allows you to set environment variables and makes them available to all child processes started from the current shell. Whenever you create a variable in a shell, it’s only available in that shell session.
With the help of export command, you can make this variable accessible to any new applications or script you run from that shell. This process is pretty useful in case you want to set up environment variables that need to be consistent across different programs. For example, you can use export command to set the PATH variable so that your system will know where to look for executable files.
Table of Contents
Here is a comprehensive guide to the options available with the export command −
- Syntax for export Command in Linux
- Different Options Available for export Command
- Examples of export Command in Linux
Syntax for export Command in Linux
The basic syntax to use the export command on Linux is given below −
export VARIABLE_NAME=value
Where,
- VARIABLE_NAME is the name of the environment variable that you want to set on the system.
- value is the value that you want to assign to the variable.
Different Options Available for export Command
With export command, there are a few options you can use to modify its behavior. You can find these options with their description in the table given below −
Option | Description |
---|---|
-f | Exports functions instead of variables, making shell functions available to child processes. |
-n | Removes the export property from a variable, meaning it will no longer be passed to child processes. |
-p | Displays a list of all exported variables and functions in the current shell. |
Examples of export Command in Linux
Let’s explore a few common examples of export command in Linux system −
- Setting an Environment Variable
- Adding a Directory to the PATH
- Exporting Multiple Variables
- Removing an Export Variable
- Listing All Exported Variables
Setting an Environment Variable
One of the important functions of export command on Linux is to set up environment variables. For example, let’s consider the following command:
export MY_VAR="HelloWorld"
When you execute the above command in the terminal, it will set the environment variable MY_VAR to “HelloWorld”. Thus, makes it available to all child processes started from the current shell.
Adding a Directory to the PATH
Another common use of export command on Linux is to add a directory to the PATH variable. This will help the system to locate the executables in that directory. For example −
export PATH=$PATH:/usr/local/mydir
Once you run the above command, it will append /usr/local/mydir to the existing PATH variable and allow the system to locate executables in that directory. You must modify the location according to your choice.
Exporting Multiple Variables
You can also export multiple variables with the export command simultaneously. For example, consider the below-given command −
export VAR1="Value1" VAR2="Value2"
This command will set and export both VAR1 and VAR2 in a single line and make them available to child processes.
Removing an Exported Variable
In case you need to stop exporting a variable, you can simply use the -n option with the export command. For example −
export -n MY_VAR
Once you use the above command, it will remove the export attribute from MY_VAR, so that it will no longer be passed to child processes.
Listing All Exported Variables
If you want to see the list of all the currently exported variables and functions on your Linux system, you can use the -p option with the export command. For example −
export -p
As soon as you execute the above command, it will display a list of all exported variables and functions in the current shell. This will help you in keeping track of your environment settings.
These examples will give you a clear understanding of how to use the export command in various scenarios on a Linux system.
Conclusion
The export is a powerful command used in Linux for managing environment variables. It is pretty useful for setting variables that are accessible to all child processes. With this command, you can easily configure your environment, such as the PATH variable to include directories where executables files are located.
This tutorial has covered the basics of export command on Linux, including its syntax, different options and a few examples for better understanding. You should follow these examples and modify them based on your requirements. This will help enhance your ability to manage and maintain the environment on your Linux system.