Wednesday, January 30, 2013

Learning Linux Commands: export

http://how-to.linuxcareer.com/learning-linux-commands-export

1. Introduction

The export command is one of the bash shell BUILTINS commands, which means it is part of your shell. The export command is fairly simple to use as it has straightforward syntax with only three available command options. In general, the export
command marks an environment variable to be exported with any newly
forked child processes and thus it allows a child process to inherit all
marked variables. If you are unsure what this means read on, as this
article will explain this process in more detail.

2. Frequently Used Options

-p List of all names that are exported in the current shell

-nRemove names from export list

-fNames are exported as functions

3. Export basics

Think over the following example:

$ a=linuxcareer.com
$ echo $a
linuxcareer.com
$ bash
$ echo $a
$

Line 1: new variable called "a" is created to contain string "linuxcareer.com"

Line 2: we use echo command to print out a content of the variable "a"

Line 3: we have created a new child bash shell

Line 4: variable "a" no longer have any values defined

From the above we can see that any new child process forked from a
parent process by default does not inherit parent's variables. This is
where the export command comes handy. What follows is a new version of the above example using the export command:

On the line 3 we have now used the export
command to make the variable "a" to be exported when a new child
process is created. As a result the variable "a" still contains the
string "linuxcareer.com" even after a new bash shell was created. It is
important to note that, in order to export the variable "a" to be
available in the new process, the process must be forked from the parent
process where the actual variable was exported. The relationship
between the child and parent process is explained below.

4. Child vs Parent process

In this section we briefly explain the relationship between the child
and parent process. Any process can be a parent and child process at
the same time. The only exception is the init process, which is always marked with PID ( process ID ) 1. Therefore, init is a parent of all processes running on your Linux system.

$ ps -p 1
PID TTY TIME CMD
1 ? 00:00:02 init

Any process created will normally have a parent process from which it
was created and will be considered as a child of this parent process.
For example:

Line 4: with use of the ps command print the child process of PID 27861

When creating a new child process an export command simply ensures that any exported variables in the parent process are available in the child process.

5. Using export command

Now that we have learned some basics we can continue to explore the export command in more detail. When using the export
command without any option and arguments it will simply print all names
marked for an export to a child process. This is the same when using
the -p option:

As you can see, once the MYVAR variable is exported it will show up
in the list of exported variables ( line 4 ). The above example can be
shortened by using the export command directly with variable assessment.

$ export MYVAR=10
$ export | grep MYVAR
declare -x MYVAR="10"

The most common use of the export command is when defining the PATH shell variable:

export PATH=$PATH:/usr/local/bin

In the example above, we have included additional path /usr/local/bin to the existing PATH definition.

6. Exporting a shell function

With the option -f the export command can also be used to export functions. In the example below, we will create a new bash function called printname, which will simply use the echo command to print the string "Linuxcareer.com".

8. Conclusion

This article covered basic use of the export command. For more information execute command:

$ man export

Make sure you tune in to our Linux jobs
portal to stay informed about the latest opportunities in the field.
Also, if you want to share your experiences with us or require
additional help, please visit our Linux Forum.