Whenever you start a new shell session (by opening a terminal), a set of environment variables is loaded from the shell configuration file into your current shell session.
These environment variables determine different customizations in your shell, like the default editor using “$EDITOR
“, or setting “$PATH
“, where your shell will search for system binaries.
The values of these variables are predefined by your system, but you can easily manipulate them and load the modified changes in your current shell session without restarting or opening a new terminal session using the export command.
In layman’s terms, the export command is used to create environment variables or update variable values in the current shell session by newly forked child processes without starting a new shell session.
Tutorial Details
Description | Export |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | export, unset |
Internet Required | No |
Syntax of the Export Command
The export command will either take an option or an environment variable in “[KEY=VALUE]
” format.
$ export [OPTION] [KEY=VALUE]
Displaying All Exported Environment Variables
The export command without any arguments will display all the exported environment variables of your system.
$ export
Output:
Displaying All Exported Environment Variables on Current Shell
The “-p
” flag will display all the exported environment variables used in the current shell.
$ export -p
Output:
Setting a New Environment Variable
To create a new environment variable in your current shell session, specify it in “[KEY=VALUE]
” format and pass it as an argument to the export command.
$ export site=linuxtldr
$ echo $site
Output:
Multiple environment variables can be easily created by specifying their names with values, using a space as a separator.
$ export site=linuxtldr cat=linux
$ echo $site $cat
Output:
If the variable is already initialized, then specify its name as an argument to the export command.
$ x=6
$ y=6
$ export x y
$ echo $x $y
Output:
You can evaluate the variable values (like adding them together) as shown.
$ echo $(($x+$y))
Output:
Update Existing Environment Variables
I’ve told you earlier that shell loads a few predefined environment variables in your current shell session, like “$EDITOR
” or “$PATH
“. However, you can easily update the value of these predefined variables using their variable names.
For example, the following command will set Vim as the default editor for your current shell session using the “$EDITOR
” variable, and Vim binary path is specified using the which command.
$ export EDITOR=$(which vim)
$ echo $EDITOR
Output:
Another example,
If you have a folder in your home directory named “script
“, that includes all the script that you want to execute from anywhere without specifying the relative or absolute path.
Than add the directory to your $PATH environment variable using the following command:
$ export PATH="$HOME/bin:$PATH"
$ export $PATH
Output:
Exporting Functions to Environment Variables
You can easily export a function to the environment variable using the “-f
” flag. But before, exporting, you need to specify a unique function name that will be used to call the function.
For example, the following is the basic function with the name “myfunc
” which will print “Hi, Linux TLDR
” when called.
$ myfunc() { echo "Hi, Linux TLDR"; }
To export this function to an environment variable, specify the function name as an argument to the export command with the “-f
” flag.
$ export -f myfunc
To call the function, enter its name.
$ myfunc
Output:
Removing a Variable from the Exported List
Use the “-n
” flag with the variable name to remove the exported variable from the list.
$ export -n site
Note that this will not reset or flush the variable value; instead, it will turn the exported global variable into a local variable.
Specify the variable or function name to unset command to delete the value as well as remove it from the exported list:
$ unset site
Make Exported Variables Permanent
All the ways you have learned in this article to export a variable or function to the exported list are temporary.
Once your terminal session or system is restarted, all the variables and their values will be cleared, and if you want them back, you need to specify them again.
To make the changes permanent, add the command (ex: “export ...
“) with the variable or function in your shell configuration file, like “~/.bashrc
” for Bash, “~/.zshrc
” for ZSH, and “~/.config/fish/config.fish
” for Fish.
And then use the source command to apply the changes to your current shell environment.
That was the end of the article.
Any suggestions or additions to this article are welcome, and we encourage you to mention them in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.