Export command in Ubuntu
Commands

Understanding the export Command in Ubuntu

Pratham Patel
Pratham Patel

Table of Contents

The export command is used for exporting environment variables so that the variable is also available to subshells and scripts you run.

You can simply export a shell variable with assignment:

export shellvariable=100

If you already have an existing variable, export it like so:

shellvariable=120

export shellvariable

Both methods demonstrated above have the same result.

But what does the export command do? Let's see in detail with examples.

What is the export command in Linux?

The export command is a "shell builtin" command. The main purpose of the export command is to make a variable available for the child process and subshells. An exported variable is also said to be a global environment variable.

What do I mean by this? Let me explain this with an example.

I have a variable greet with the string "Hello world" in it.

$ greet="Hello world"

Now I want to run a shell script that uses this greet variable?

#!/usr/bin/env bash

echo $greet

But if I run the shell script, it shows nothing:

$ ./test.sh

$

But running echo $greet works.

$ echo $greet
Hello world

Then why does it not run when it is executed from a script? This is because the greet variable is not available inside the test.sh script.

Let's do the same thing, but this time, I will use the export command.

$ greet = "Hello world"

$ export greet

$ ./test.sh
Hello world

Ah, so it works now?!

This shows the use of the export command. The export command exported the variable greet. It became a global variable and because of that, it was accessible by the test.sh script.

🙌
You can use the printenv command to see your shell's exported environment variables. Even if you didn't export them explicitly, you'd still find variables set by the system.

"Un-exporting" a shell variable

What if you wish to undo the action of exporting a function/variable?

To do so, use the -n option with the export command. Below is the syntax to "un-export" a shell variable:

export -n VAR

Assume I have a variable extravar exported as an environment variable. Now I wish to "un-export" it. I will do so in the following manner:

$ printenv | grep extravr
extravar=420

$ export -n extravar

$ printenv | grep extravr

As you can see, previously, I had the extravar variable exported with the value of '420' set. Then I used the -n option to remove that variable from the list of exported of variables. Now, checking if it is present in the output of printenv command, I can see that it is not present. Hence, it is now "un-exported".

Permanently exporting a variable

The exported variable is available to all the subshell as long as the shell is active. If you log out of the parent shell or close the terminal or shut down the system, your exported variable is lost. You'll have to export it again.

Repetition of manually declaring and exporting the variable every time you want to use it can get tiresome.

To make it less tiresome, you can add the export command to your shell's resource file. If you are using bash, your shell's resource file is .bashrc and for Zsh users, it is the .zshrc file.

Let's say I wish to always export a few variables. These variables are USERNAME, PASSWORD and SECRET_KEY. To export them, I will add the following lines to my .bashrc file:

export USERNAME="pratham"
export PASSWORD="12345678 is a poor password"
export SECRET_KEY="mysecret"

Doing so will automatically export these three variables as soon as a new shell starts. This is usually when you open a new terminal window or a new tab in your terminal.

Exporting a shell function

The export command is not limited to variables. The export command can also be used to export shell functions so that – like exported variables – they too are available to the child process.

To export a shell function, the -f option needs to be used with the export command. By default, the export command will assume that the name given to it is a shell variable.

export -f FUNC

Assume the following shell function:

myfunc() {
	echo "Hello there, I assume your name is $name"
}

I have this function declared, and I wish to access it from the sh_fn.sh shell script. The contents of this shell script are as follows:

#!/usr/bin/env bash

myfunc

Here, all I am doing is calling the myfunc function. Let us see how to export the myfunc function to access it from the script.

$ myfunc() {
>	echo "Hello there, I assume your name is $name"
> }

$ export -f myfunc

$ export name="Pratham"

$ ./sh_fn.sh
Hello there, I assume your name is Pratham

I declared the myfunc function and later on exported it using the -f option. This tells the export command to treat this name as a shell function. Later on, I define the name variable and export it as well.

Finally, I run the shell script and get expected result.

If you wish to remove a shell function, the -f option is required. The syntax is as follows:

export -n -f FUNC

Conclusion

This article covers how you can use the export command to export a shell variable or a shell function as an environment variable and use it in child processes. I also covered the removal of exported environment variables. Let me know if you have questions.