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
  • -n
    Remove names from export list
  • -f
    Names 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:
$ a=linuxcareer.com
$ echo $a
linuxcareer.com
$ export a
$ bash
$ echo $a
linuxcareer.com
$
Linux Career Internal
 advertisementOn 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:
$ echo $$
27861
$ bash
$ echo $$
28034
$ ps --ppid 27861
  PID TTY          TIME CMD
28034 pts/3    00:00:00 bash
  • Line 1: print a PID for a current shell - 27861
  • Line 2: create a new child process from the process ID 27861
  • Line 3: print a PID for a current shell - 28034
  • 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:
$ export
declare -x COLORFGBG="15;0"
declare -x DEFAULTS_PATH="/usr/share/gconf/cinnamon.default.path"
declare -x DESKTOP_SESSION="cinnamon"
declare -x DISPLAY=":0".....
As shown previously, to export a variable we simply use the  variable's name as an argument to an export command.
$ MYVAR=10
$ export | grep MYVAR
$ export MYVAR
$ export | grep MYVAR
declare -x MYVAR="10"
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".
$ printname () { echo "Linuxcareer.com"; }
$ printname
Linuxcareer.com
$ export -f printname
$ bash
$ printname
Linuxcareer.com

7. Removing names from export list

 Following the example above we now have the MYVAR variable defined in our export list.
$ export | grep MYVAR
declare -x MYVAR="10"
To remove this variable from the export list we need to use the -n export option.
$ export | grep MYVAR
declare -x MYVAR="10"
$ export -n MYVAR
$ export | grep MYVAR
$

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.

No comments:

Post a Comment