Linux environment variable Export command details

Source: Internet
Author: User

Problems caused by host $ export dvsdk = "$ {home}/ti-dvsdk_dm368-evm_xx_xx_xx_xx"

1,$ {Home}: first, home is a variable that represents your home directory. The $ symbol must be added before the variable; otherwise, an error is reported.

Usr/local/dvsdk: the destination folder of dvsdk is added by # echo $ dvsdk test environment variable

2,To enable the environment variable to be automatically loaded at startup, you can write it to/etc/Re. Local.

Linux Export command

Function Description:Set or display environment variables. (For example, we need to use a command, but the execution file of this command is not in the current directory. In this way, we must specify the directory of the execution file every time we use it. In this case, execute export first in the code, this is equivalent to telling the program that the files or things needed to execute things are in these directories)

Syntax:Export [-FNP] [variable name] = [variable setting value]

Note:When executing a program in shell, shell provides a set of environment variables. Export allows you to add, modify, or delete environment variables for subsequent programs. The effect of export is only true for this login operation.

Parameters:

-F indicates the function name in [variable name.

-N: Delete the specified variable. The variable is not actually deleted, but is not output to the execution environment of subsequent commands.

-P: list all environment variables that the shell assigns to the program.

When a variable is created, it is not automatically known to the shell process created after it. The command export can pass the variable value to the shell in the back. When a shell script is called and executed, it does not automatically obtain access to the variables defined in the original script (caller) unless these 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

 

How to Set environment variables in Linux (export path)

Generally, you need to specify the path of the compilation tool when configuring the cross-compilation tool chain. In this case, you need to set the environment variable. For example, in the "/opt/au1200_rm/build_tools/bin" Directory of my MIPS-Linux-GCC compiler, build_tools is my compilation tool. There are three ways to set the environment variables:

1. directly use the Export command:
# Export Path = $ path:/opt/au1200_rm/build_tools/bin
Check whether a port has been set. Run the Export command to view the Port:

 

[Root @ localhost 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.20.2"
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"

We can see that the gray part has a set path, indicating that the environment variable has been set, and the path of the compiler that I want to add is already in the path.

2. Modify the profile file:
# Vi/etc/profile
Add:
Export Path = "$ path:/opt/au1200_rm/build_tools/bin"

3. Modify the. bashrc file:
# Vi/root/. bashrc
Add:
Export Path = "$ path:/opt/au1200_rm/build_tools/bin"

The last two methods generally take effect after you log out of the system again. Finally, you can test them using the echo command:
# Echo $ path
Check whether the path/my_new_path already exists in the output.

In addition:4. Modify the/etc/Re. Local file:
# Vi/etc/Re. Local
Add:
Export Path = "$ path:/opt/au1200_rm/build_tools/bin"

Bytes -----------------------------------------------------------------------------------------------------------------------

 "/Bin", "/sbin", "/usr/bin", "/usr/sbin", "/usr/local/bin", and other paths are already in the system environment variables. now, if the executable file is in these standard locations, enter the file name and parameters of the executable file of the software in the terminal command line (if you need parameters), press Enter.

If it is not in the standard position, you must add the complete path before the file name. However, it is too troublesome to run like this every time. A "once and for all" method is to add this path to the environment variable. CommandExport $ Path = "path" (or"Path = $ path: path")($ Path is the environment variable name, such as dvsdk. You can add this path to the environment variable when calling $ dvsdk. However, if you exit this command line, it becomes invalid. To take effect permanently, add this line to the environment variable file. Two files are available.: "/Etc/profile" and ". bash_profile "and"/etc/profile "are valid for all users in the system. bash_profile "is only valid for this user.

  Export $ Path = "$ path: Path 1: Path 2:...: path N" (or"Path = $ path: Path 1: Path 2:...: path N"),The path of the executable file includes the previously set path, and all paths from "Path 1" to "path N. After you enter a string of characters and press enter, shell finds the corresponding executable files in these paths and submits them to the system core for execution. The "$ path" indicates that the previously set path is still valid. Do not miss it. Some software may have environment variables other than "path" that need to be added, but the method is the same, and you also need to pay attention to "$ ".

Note: Unlike DOS/window, path names in Unix-type system environment variables are separated by colons instead of semicolons. In addition, the more software is installed, the more environment variables are added. To avoid confusion, we recommend that you add all statements at the end of the file and add them in the software installation order.

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 a annotator, which has no effect except visual separation.

After setting, log out and log on again. The setting takes effect. If you do not deregister a statement, it can take effect if you execute these statements directly in the shell, but the scope is limited to the shell that executes these statements.

After the related environment variables take effect, you do not have to go to the executable file directory of the software to perform operations.

 

Bytes -----------------------------------------------------------------------------------------------------------------------

When executing a script, a sub-shell environment is started first (I don't know if this is the case for executing other programs), and then all the system environment variables in the parent shell are copied, the statements in this script are executed in the sub-shell. (That is, the environment variable of the parent shell can be called in the sub-shell, but in turn it won't work. If the environment variable is defined in the sub-shell, it is only valid for the shell or its sub-shell. When the sub-shell ends, it can also be understood that the variable disappears after the script is executed .) To prove this, see the script content:
Test = 'value'
Export Test
After such a script is executed, test does not actually exist. Next, let's look at the following:
Test = 'value'
Export Test
Bash
Here we open another sub-shell in the last line of the script, which should be the sub-shell of the shell where the script file is located. After the script is executed, we can see the variable test, because it is in its sub-shell, when exit is used to exit the sub-shell, the test variable disappears.
If you use source to execute the script without export, you will not see this variable in the sub-shell, because it is not a system environment variable. For example, the script content is:
Test = 'value'
After executing the command with source, you can see this variable in shell, but when executing Bash to open a sub-shell, test will not be copied to the sub-shell, because the execution script file is actually run in a sub-shell, When I create another script file for execution, nothing will be input, such as Echo $ test. So pay special attention to this. We can use echo $ test to output the variable value at the prompt. Why can't we put it into the script file?
The conclusion is as follows: 1. When a script is executed, it runs in a sub-shell environment. After the script is executed, the sub-shell automatically exits; 2. system environment variables in a shell will be copied to the sub-shell (variables defined using export ); 3. The system environment variables in a shell are only valid for the shell or its sub-shell. When the shell ends, the variables disappear (and cannot be returned to the parent shell ). 3. Variables not defined by export are only valid for this shell and are also invalid for the sub-shell.
Later, according to the prompts of the Moderator, I sorted out the post: Why is one script executed directly and another line executed with source? This is also a problem I encountered. Manual: read and execute commands from filename in the Current Shell environment and return the exit status of the Last Command executed from filename. Do you understand why? Directly executing a script file runs in a sub-shell, while source runs in the Current Shell environment.

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.