You can effectively change a shell variable into an environment variable by using the export command. For example, if you have a shell variable SHL_VAR, you can enter export SHL_VAR to make it an environment variable. You can also change the value of a variable while exporting it, including existing environment variables. You can do this by entering something similar to export SHL_VAR=”New value” at the CLI. This will set the value for all child processes spawned from this shell.
In order to set the value of an environment variable for all future Bash sessions, you can add an export statement to your .bash_profile file. To automate this process for new users, and to ensure those with a similar job roles have the same environment variable settings, you can modify the .bash_profile file in the /etc/skel/ directory. To set the value of an environment variable system-wide, add an export statement to the appropriate file in the /etc/profile.d/ directory.
Syntax
The syntax of the export command is:
# export [options] [NAME[=value]]
If you encounter the below error while running the export command:
export: command not found
you may try installing the below package as per your choice of distribution:
OS Distribution | Command |
---|---|
Debian | apt-get install execline |
Ubuntu | apt-get install execline |
Alpine | apk add execline |
Kali Linux | apt-get install execline |
export Command Examples
1. Set a new environment variable:
# export VARIABLE=value
2. Remove an environment variable:
# export -n VARIABLE
3. Mark a shell function for export:
# export -f FUNCTION_NAME
4. Append something to the PATH variable:
# export PATH=$PATH:path/to/append