Learning Linux Commands: export
Rabu, 30 Januari 2013
0
komentar
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
$ a=linuxcareer.comOn 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.
$ echo $a
linuxcareer.com
$ export a
$ bash
$ echo $a
linuxcareer.com
$
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 1Any 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:
PID TTY TIME CMD
1 ? 00:00:02 init
$ 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
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:$ exportAs shown previously, to export a variable we simply use the variable's name as an argument to an export command.
declare -x COLORFGBG="15;0"
declare -x DEFAULTS_PATH="/usr/share/gconf/cinnamon.default.path"
declare -x DESKTOP_SESSION="cinnamon"
declare -x DISPLAY=":0".....
$ MYVAR=10As 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 | grep MYVAR
$ export MYVAR
$ export | grep MYVAR
declare -x MYVAR="10"
$ export MYVAR=10The most common use of the export command is when defining the PATH shell variable:
$ export | grep MYVAR
declare -x MYVAR="10"
export PATH=$PATH:/usr/local/binIn 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 MYVARTo remove this variable from the export list we need to use the -n export option.
declare -x MYVAR="10"
$ 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 exportMake 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.
TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: Learning Linux Commands: export
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke http://androidjapane.blogspot.com/2013/01/learning-linux-commands-export.html. Terima kasih sudah singgah membaca artikel ini.Ditulis oleh Unknown
Rating Blog 5 dari 5
0 komentar:
Posting Komentar