Linux environment variable Export command detailed

Source: Internet
Author: User
Tags echo command

Issues raised by the host $ export dvsdk= "${home}/ti-dvsdk_dm368-evm_xx_xx_xx_xx"

1,${home}: First, home is a variable, it represents your home directory, the variable must be preceded by a $ symbol, otherwise it will be an error

USR/LOCAL/DVSDK for DVSDK destination folder was added successfully by #echo $DVSDK test environment variable No

2, want to enable the automatic loading of this environment variable at boot time after each set, you can write it/etc/re.local

Linux Export Command

Feature Description: Sets or displays environment variables. (for example, we want to use a command, but the execution of this command is not in the current directory, so we must specify the directory to execute the file each time we use, trouble, in the code to execute the export, this is equivalent to tell the program, execute a certain thing, the required files or something in these directories)

syntax:export [-fnp][variable name]=[variable setting value]

Additional Note: when executing a program in the shell, the shell provides a set of environment variables. Export can add, modify, or delete environment variables for use by subsequent executing programs. The effect of export only extends to the operation of this landing.

Parameters

-F represents the function name in [variable name].

-n Deletes the specified variable. The variables are not actually deleted, but are not exported to the execution environment of the subsequent directives.

-p lists all the environment variables that the shell assigns to the program.

When a variable is created, it is not automatically known to the shell process that was created after it. The command export can pass the value of the variable to the following shell. When a shell script is called and executed, it does not automatically get access to the variables defined in the script (the caller) unless the variables have been explicitly set to available.     The export command can be used to pass the value of one or more variables to any subsequent script. ----"UNIX Tutorial"

  methods for setting environment variables in Linux ( Export PATH)

In general, you need to specify the path of the compilation tool when configuring the cross-compilation tool chain, and you need to set the environment variables. For example, my MIPS-LINUX-GCC compiler in the "/opt/au1200_rm/build_tools/bin" directory, Build_tools is my compilation tool, there are three ways to set the environment variables:

1. Direct with export command:
#export path= $PATH:/opt/au1200_rm/build_tools/bin
To see if it is already set up, use the command export to view:


[Email protected] bin]#Export
Declare-x bash_env= "/ROOT/.BASHRC"
Declare-x g_broken_filenames= "1"
Declare-x histsize= "1000"
Declare-x home= "/root"
Declare-x hostname= "Localhost.localdomain"
Declare-x inputrc= "/ETC/INPUTRC"
Declare-x lang= "ZH_CN. GB18030 "
Declare-x language= "Zh_CN.GB18030:zh_CN.GB2312:zh_CN"
Declare-x lessopen= "|/usr/bin/lesspipe.sh%s"
Declare-x logname= "Root"
Declare-x ls_colors= "no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi= 01;05;37;41:ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.sh=01;32:*.csh=01;32:*.tar= 01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*. Z=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01 ; 35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35: "
Declare-x mail= "/var/spool/mail/root"
Declare-x oldpwd= "/opt/au1200_rm/build_tools"
Declare-x path= "/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/x11r6/bin:/root/bin:/opt/ Au1200_rm/build_tools/bin "
Declare-x pwd= "/opt/au1200_rm/build_tools/bin"
Declare-x shell= "/bin/bash"
Declare-x shlvl= "1"
Declare-x ssh_askpass= "/usr/libexec/openssh/gnome-ssh-askpass"
Declare-x ssh_auth_sock= "/tmp/ssh-xx3lkwhz/agent.4242"
Declare-x ssh_client= "10.3.37.152 2236 22"
Declare-x ssh_connection= "10.3.37.152 2236 10.3.37.186 22"
Declare-x ssh_tty= "/DEV/PTS/2"
Declare-x term= "Linux"
Declare-x user= "Root"
declare-x username= "root"


You can see the gray section has a set path, indicating that the environment variable has been set up, path has the compiler I want to add the path.

2, modify the profile file:
Add in:
opt/au1200_rm/build_tools/bin
3. Modify the. bashrc file:
Add in:
/opt/au1200_rm/build_tools/bin
See if the output is already/my_new_path this path.

4/etc/re.local file:
            added:
Span style= "Font-family:courier new; font-size:14px; " >            export PATH= "$PATH: /opt/au1200_rm/build_tools/bin

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

 "/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/local/bin" and other paths are already in the system environment variables, if the executable file in these standard locations, in the terminal command line enter the software executable file name and parameters ( If parameters are required), enter.

If not in a standard location, the file name needs to be preceded by a full path. But it's too much trouble to run like this every time, a "once and for all" approach is to add this path to the environment variable. Command Note that, unlike Dos/window, the UNIX class system environment variable has the following format ():   

# Software Name-version number

Path= $PATH: Path 1: Path 2: ...: path n

Other environment variables =$ other environment variables: ...

In "Profile" and ". Bash_profile", "#" is a comment symbol that is written here without any effect except visual separation.

Setup is complete, log off and log back in, and the settings take effect. If you do not log off, executing these statements directly in the shell can also take effect, but the scope is limited to the shell that executes the statements.

Once the relevant environment variable is in effect, you don't have to run into the software's executable directory.

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

 

When executing a script, a child shell environment is opened (not knowing that other programs are executing), and then all the system environment variables in the parent shell are copied, and the statements in the script are executed in the child shell. (That is, the environment variable of the parent shell can be called in the child shell, but not the other way, if the environment variable is defined in the child shell, it is only valid for the shell or its child shell, and when the child shell ends, the variable disappears when the script finishes.) To prove this, look at the script content:
test= ' value '
Export Test
When such a script finishes executing, test does not actually exist. Then look at the following:
test= ' value '
Export Test
Bash
Here in the last line of the script to open a child shell, the shell should be the shell of the script file is the child shell, after the script is executed, you can see the test variable, because now is in its child shell, when exit child shell with exit, The test variable disappears.
If you execute the script with source, you will not see this variable in the child shell if you do not add the export, because it is not a system environment variable, such as the script content is:
test= ' value '
After executing with source, you can see this variable under the shell, but when you execute bash to open a child shell, test is not copied to the child shell, because the execution script file is actually run in a child shell, so when I build another script file to execute, Will not enter anything, such as: Echo $test. So this particular attention, obviously at the prompt can use echo $test output variable value, why put it into the script file is not?
So the conclusion is: 1, the script is executed in a child shell environment run, the script after the execution of the child shell automatically exit, 2, a shell of the system environment variables will be copied to the child shell (the variable defined by export); 3, A system environment variable in a shell is valid only for the shell or its child shell, and the shell ends when the variable disappears (and cannot be returned to the parent shell). 3. Variables defined without export are valid only for the shell, and the child shell is not valid.
Later, according to the moderator's tips, organized a few posts: why a script directly executed and with the source to execute a line? This is also a problem I have encountered. Manual Original Is this: Read and execute commands from filename in the current shell environment and return the exit status of the LA St command executed from filename. You understand why it's not the same? Executing a script file directly is run in a child shell, while source is running in the current shell environment.

Linux environment variable Export command detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.