Jump to content

Bash Export Command


Linux Hint

Recommended Posts

“Bash shell offers the export command, a built-in command that allows exporting variables in a shell to make them global, such that you can access them from another shell. With the export command, you export the environmental variables to other shells as a child process without tampering with the existing environmental variables. This guide discusses the Bash export command, giving examples of how you can use it.”

Understanding the Bash Export Command

The export command has only three options.

  1. -n: used to indicate the named variables won’t be exported
  2. -p: used to list all exported variables and functions
  3. -f: used when exporting functions

Let’s have an example to understand when and where the export command comes in handy. Suppose you are in the current shell and create a local variable named demo1, as shown below.

bash_export_command-01.png

If we want to access the variable’s value, we could use the echo command.

$ echo $demo1

bash_export_command-02.png

However, if we opened another shell using the bash command and tried to get the value of the declared variable in the other shell, we got nothing as an output.

bash_export_command-03.png

This happens because the variable we created was local to the given shell. If we want to make it global, we must export it. Let’s return to our previous shell and export the variable using the below command.

$ exit
$ export demo1

bash_export_command-04.png

If we go to a new shell and echo the variable, we see that we now get a value, meaning we have access to the variable.

bash_export_command-05.png

That’s all possible because we exported it using the export command.

Viewing Exported Variables

If you run the export command with no arguments, it will list all the exported variables in your system regardless of the shell.

bash_export_command-06.png

However, if you add the -p option, it will only list the exported variables in the current shell

bash_export_command-07.png

Notice that we have the variable we exported at the bottom of the list.

bash_export_command-08.png

Exporting Functions

You can go beyond variables to export even functions with the export command. To make the shell recognize that you are exporting a function, use the -f flag.

Let’s create a function, export it in the current shell, then try to access it in another shell and see if that works.

bash_export_command-09.png

With our function created, we can verify that it’s unavailable in the new shell, as shown below, where it returns an error.

bash_export_command-10.png

So, go back and export it using the -f flag.

bash_export_command-11.png

Lastly, open a new shell and verify that our function is now global.

bash_export_command-12.png

Bingo! You managed to export a function.

Setting Values

You can set the value of a variable using the export command. For this, you need the syntax below.

$ export name[=value]

For instance, if we wanted to change the value of the variable we created, we could use the below command.

bash_export_command-13.png

Once updated, echo the variable to check if it reflects the new value.

bash_export_command-14.png

That’s how you can manipulate other environmental variables, such as setting the default editor. You only need to grep the currently set editor and set a new one.

Let’s check the current default editor using the command below.

$ export | grep EDITOR

bash_export_command-15.png

In this case, we have no currently set default editor. Let’s go ahead and set one like vim or nano using the command below.

$ export EDITOR=/usr/bin/nano

bash_export_command-16.png

If we check again, we see that the variable value was modified.

Conclusion

This article has focused on understanding the bash export command and how to use it. We’ve given various examples of how to use the export command options to export variables and functions. Knowing how to use the export command comes in handy when working with bash scripts. So, don’t stop here. Keep practicing!

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...