Analysis of the LINUX environment variable export order detailed _linux

Source: Internet
Author: User
Tags echo command ssh

Problems caused 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 precede the $ symbol, otherwise it will be an error

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

2, want to make the boot automatically load this environment variable after each setting, 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 file for this command is not in the current directory, so we have to specify the directory of the execution file every time we use it, and in the code, we'll run export first, which is equivalent to telling the program that the file or whatever is needed to execute something.

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

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

Parameters

-F represents the function name in the variable name.

-n Deletes the specified variable. Variables are not actually deleted, but are not exported to the execution environment of 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. and command export can pass the value of a variable to a later shell. When a shell script is invoked and executed, it does not automatically gain access to the variables defined in the script (caller) unless the variables have been explicitly set to be available.     The export command can be used to pass the value of one or more variables to any subsequent script. ----"The Unix Tutorial"

How to set environment variables in Linux (export PATH)

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

1. Direct use of export command:

#export path= $PATH:/opt/au1200_rm/build_tools/bin

Check to see if it is already set up and check it out with the command export:

[Root@localhost bin] #export declare-x bash_env= "/ROOT/.BASHRC" Declare-x g_broken_filenames= "1" declare-x HISTSIZE= "1 "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" Declare-x ssh_tty= "/DEV/PTS/2" Declare-x term= "Linux" Declar  E-x user= "root" declare-x username= "root"

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

2, modify profile file:

#vi/etc/profile

In the inside add:

Export path= "$PATH:/opt/au1200_rm/build_tools/bin"

3. Modify the. bashrc file:

# VI/ROOT/.BASHRC

In the inside add:

Export path= "$PATH:/opt/au1200_rm/build_tools/bin"

The latter two methods generally require a cancellation of the system in order to take effect, and finally can be tested by the echo command:

# echo $PATH

See if there is already a/my_new_path this path in the output.

The other is: 4. To modify the/etc/re.local file:

# vi/etc/re.local

In the inside add:

 Export path= "$PATH:/opt/au1200_rm/build_tools/bin"

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

Paths such as "/bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/local/bin", etc., are already in the system environment variable, and if the executable file is in these standard locations, enter the file name and parameters for the software executable at the terminal command line ( If you need a parameter, enter.

If you are not in a standard location, you need to add the full path before the file name. But every time it's too much trouble to run, a "once and for all" approach is to add this path to the environment variables. Command Export $PATH = "path" (or "path= $PATH: Path") ($PATH the name of the environment variable, such as DVSDK; call $DVSDK) can add this path to the environment variable, but the exit command line is invalidated. To be effective permanently, you need to add this line to the environment variable file. There are two files available: "/etc/profile" and ". Bash_profile" in the user's home directory, "/etc/profile" is valid for all users in the system, and ". Bash_profile" in the user's home directory is only valid for this user.

Export $PATH = "$PATH: Path 1: Path 2:.: Path N" (or "path= $PATH: Path 1: Path 2: ...: path n"), meaning that the path to the executable file includes the path originally set, and includes all paths from "Path 1" to "Path N". When the user enters a string of characters and presses the carriage return, the shell in turn will find the corresponding executable file in these paths and give it to the system core for execution. The "$PATH" indicates that the previously set path is still valid and that you should not omit it. Some software may also have an environment variable of a type other than "PATH" that needs to be added, but the method is the same, and you also need to be aware of "$".

Note that unlike Dos/window, the pathname in the UNIX class system environment variable is separated by a colon, not a semicolon. In addition, the more installed software, the more environment variables, in order to avoid confusion, we recommend that all statements are added at the end of the file, in the order in which the software is installed.

The format is as follows ():

# Software Name-version number

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

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

In "Profile" and ". Bash_profile", "#" is an annotation symbol, written here without any effect except for visual separation.

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

After the relevant environment variables are in effect, you do not have to run into the software executable directory to operate.

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

When executing a script, a child shell environment is first opened (not knowing if the other program is 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 invoked in the child shell, but not the other way, if the environment variable is defined in the child shell, only the shell or its child shell is valid, and when the child shell ends, the variable disappears when the script finishes executing.) To prove this, look at the script content:

test= ' value '
export test

After such a script is executed, test does not actually exist. Then look at the following:

test= ' value '
Export test
Bash

Here in the final line of the script to open a child shell, the shell should be the script file in the shell of the shells, the script can be seen after the execution of this variable, because it is now in its child shell, when the exit with exit shell, The test variable disappears.

If the script is executed with source, the variable is not seen in the child shell without export, because it is not a system environment variable, such as the script content:

test= ' value '

When executed with source, you can see the variable under the shell, but then when you execute bash to open a child shell, test is not copied to the child shell, because the execution script file is actually running in a child shell, so when I build another script file to execute, is not going to enter anything, such as: Echo $test. So this particular attention, clearly in the prompt can use echo $test output variable value, why put it into the script file is not?

So the conclusion is: 1, the execution of the script is in a child shell environment, the script after the execution of the child shell automatically exit; 2, the system environment variables in a shell are copied into the sub shell (with the variables defined by export); 3. The system environment variable in a shell is valid only for the shell or its child shell, and the variable disappears at the end of the shell (and cannot be returned to the parent shell). 3. A variable that is not defined by export is valid only for the shell, and the child shell is also invalid.

Later, according to the moderator's tips, sorted out the post: Why a script to execute directly and with the source execution is not a row? This is also a problem that I have encountered myself. Manual The original text 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. See why it's different? Executing a script file directly is running in a child shell, and source is running in the current shell environment.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.