How to set environment variables in Linux (exportPATH)

Source: Internet
Author: User
Tags echo command
In Linux, the environment variable setting method (exportPATH) generally requires specifying 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, my mips-linux-gcc compiler is in the/opt/au1200_rm/build_tools/bin directory, bui...

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"

As you can see, the environment variable has been set, and the PATH of the compiler that I want to add already exists 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.

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. The command "PATH = $ PATH: PATH" can add this PATH to the environment variable, but exit this command line will become 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.

"PATH = $ PATH: PATH 1: PATH 2 :...: path n, which indicates that the path of the executable file includes the original 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.

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.