Shell之export命令

SHELL是用户用来方便操控【操作系统】的一个接口程序。

对于【操作系统】来说,这个接口程序就象是包在其外的一个壳:SHELL

----------------

下面对shell的基本命令:export进行解释

-----------------

Bashhasseveralcommandsthatcomeswiththeshell(i.ebuiltinsidethebashshell).

Whenyouexecuteabuilt-incommand,bashshellexecutesitimmediately,withoutinvokinganyotherprogram.

Bashshellbuilt-incommandsarefasterthanexternalcommands,becauseexternalcommandsusuallyforkaprocesstoexecuteit.

Inthisarticleletusreviewsomeusefulbashshellbuiltinswithexamples.

1.BashExportCommandExample

exportcommandisusedtoexportavariableorfunctiontotheenvironmentofallthechildprocessesrunninginthecurrentshell.

export varname=value
# exports a function in the current shell.
export -f functionname

Itexportsavariableorfunctionwithavalue.

“env”commandlistsalltheenvironmentvariables.Inthefollowingexample,youcanseethatenvdisplaystheexportedvariable.

$ export country=India

$ env
SESSIONNAME=Console
country=India
_=/usr/bin/env

“export-p”commandalsodisplaysalltheexportedvariableinthecurrentshell.

---------------------

===============

HowdoIuseexportcommandunderaLinuxorUnix-likeoperatingsystemstosetvariablesonabashshell?

Youcanexportshellvariablesusingtheexportcommand.

Syntax

Thesyntaxisasfollows:

export VAR

Youcanassignvaluebeforeexportingusingthefollowingsyntax:

export VAR=value

OR

VAR=value

export VAR

TheexportcommandwillmarkseachVARforautomaticexporttotheenvironmentofsubsequentlyexecutedcommandsi.e.makethelocalshellvariableVARglobal.

Examples

TomakethelocalshellvariablecalledPATHtypethefollowing:

### export PATH ###
export PATH=$PATH:/usr/local/bin
echo "$PATH"

SetanewEDITORvariable:

export EDITOR=/usr/bin/vim

Youneedtoaddexportstatementsto

~/.bash_profileor

~/.profileor

/etc/profile

file.

Thiswillexportvariablespermanently:

$ vi ~/.bash_profile

Samplefile

PATH=$PATH:$HOME/bin
export PATH
 
# set vim as a text editor
export EDITOR=/usr/bin/vim
 
# set colorful prompt 
export PS1='\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '
 
# set java_home
export JAVA_HOME=/usr/local/jdk

Toseeallalistofallexportedvariablesandfunctions,enter:

$ export -p

-

15UsefulBashShellBuilt-inCommands(WithExamples)

http://www.thegeekstuff.com/2010/08/bash-shell-builtin-commands/

UseexportCommandinLinux/Unix

https://www.cyberciti.biz/faq/linux-unix-shell-export-command/

-

相关推荐