Linux Settings environment variable method (export PATH) __linux

Source: Internet
Author: User
Tags echo command

1. Dynamic library path setting

Calling dynamic libraries under Linux is not the same as windows. Linux executable programs rely on configuration files to read the path, so sometimes you need to set the path

The specific operation is as follows

export LD_LIBRARY_PATH = / home / ... (the directory of the dynamic library)

However, this setting method is only valid in the current session

You can modify the configuration file to achieve any session is valid

2. Setting of environment variables

In general, when configuring the cross-compilation tool chain, you need to specify the path of the compilation tool, and you need to set environment variables at this time. For example, my mips-linux-gcc compiler is in the "/ opt / au1200_rm / build_tools / bin" directory, and build_tools is my compilation tool. There are three ways to set environment variables:

1. Use the export command directly:

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

To check whether it has been set, use the command export to view:

[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.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"

As you can see, the environment variables have been set, and the path of the compiler I want to add is already in the PATH.

2. Modify the profile file:

#vi / etc / profile

Add in:

export PATH = "$ PATH: / opt / au1200_rm / build_tools / bin"

For the environment variables to take effect immediately, the following command needs to be executed:

#source / etc / profile

3. Modify the .bashrc file:

# vi /root/.bashrc

Add in:

export PATH = "$ PATH: / opt / au1200_rm / build_tools / bin"

The latter two methods generally need to log off the system to take effect. Finally, you can test it with the echo command:

# echo $ PATH

See if the / my_new_path path already exists in the output.

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

"/ Bin", "/ sbin", "/ usr / bin", "/ usr / sbin", "/ usr / local / bin" and other paths are already in the system environment variables, if the executable files are in these In a standard location, enter the file name and parameters of the software executable file on the command line of the terminal (if parameters are required), and press Enter.

If it is not in the standard location, the full path must be added in front of the file name. But it's too much trouble to run like this every time. A "one time and one forever" solution is to add this path to environment variables. The command "PATH = $ PATH: path" can add this path to the environment variable, but exiting this command line is invalid. To take effect permanently, you need to add this line to the environment variable file. There are two files to choose from: "/ 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 for this The user is valid.

"PATH = $ PATH: path 1: path 2: ...: path n", meaning that the path of the executable file includes the originally set path, and also includes all paths from "path 1" to "path n". When the user enters a string of characters and presses Enter, the shell will sequentially find the corresponding executable file in these paths and give it to the system core for execution. The "$ PATH" indicates that the originally set path is still valid. Be careful not to miss it. Some software may have environment variables of types other than "PATH" that need to be added, but the method is the same, and you also need to pay attention to "$".

Note that unlike DOS / Window, path names in UNIX-like system environment variables are separated by colons, not semicolons. In addition, the more software is installed, the more environment variables are added. In order to avoid confusion, it is recommended that all statements be 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 a comment symbol, written here has no effect other than visual separation.

After setting, log out and log in again, the setting will take effect. If you do not log out, execute these statements directly in the shell, it can also take effect, but the scope of action is limited to the shell that executed these statements.

After the relevant environment variables become effective, you don't have to go to the executable directory of the software to operate.

from: http: //blog.csdn.net/kpgood/archive/2009/03/07/3965446.aspx

There are more and more friends using linux. The first step in development under linux is to configure environment variables. The following uses the configuration of java environment variables as an example to introduce three methods of configuring environment variables.

 

1. Modify the / etc / profile file

If your computer is only used for development, this method is recommended, because all user shells have the right to use these environment variables, which may bring security problems to the system.

 

(1) Open / etc / profile with a text editor

 

(2) At the end of the profile file, add:

JAVA_HOME = / usr / share / jdk1.5.0_05

PATH = $ JAVA_HOME / bin: $ PATH

CLASSPATH =.: $ JAVA_HOME / lib / dt.jar: $ JAVA_HOME / lib / tools.jar

export JAVA_HOME

export PATH

export CLASSPATH

 

(3) Re-login

 

annotation:

a. You need to change /usr/share/jdk1.5.0_05jdk to your jdk installation directory

 

b. Use ":" to separate paths under linux

 

c. $ PATH / $ CLASSPATH / $ JAVA_HOME is used to refer to the value of the original environment variable. When setting the environment variable, pay special attention to not overwriting the original value. This is a common mistake.

 

d. The current directory "." in the CLASSPATH cannot be lost. It is also a common mistake to drop the current directory.

 

e. export is to export these three variables as global variables.

 

f. Case must be strictly distinguished.

 

2. Modify the .bashrc file

This method is more secure. It can control the permissions of using these environment variables to the user level. If you need to give these users permission to use these environment variables, you only need to modify the .bashrc file in the home directory of the individual user. Too.

 

(1) Open the .bashrc file in the user directory with a text editor

 

(2) At the end of the .bashrc file, add:

set JAVA_HOME = / usr / share / jdk1.5.0_05

export JAVA_HOME

set PATH = $ JAVA_HOME / bin: $ PATH

export PATH

set CLASSPATH =.: $ JAVA_HOME / lib / dt.jar: $ JAVA_HOME / lib / tools.jar

export CLASSPATH

 

(3) Re-login

 

3. Set variables directly under the shell

This method is discouraged, because changing the shell will invalidate your settings, so this method is only for temporary use, and you have to reset it when you want to use it later, which is more troublesome.

 

Just execute the following command in the shell terminal:

export JAVA_HOME = / usr / share / jdk1.5.0_05

export PATH = $ JAVA_HOME / bin: $ PATH

export CLASSPATH =.: $ JAVA_HOME / lib / dt.jar: $ JAVA_HOME / lib / tools.jar

 

Under linux system, if you download and install the application, it is very likely that the prompt of “command not found” will appear when you type its name. If you go to the installation target folder every time, finding the executable file to operate is too cumbersome. This involves the setting of the environment variable PATH, and the setting of PATH is also an integral part of customizing environment variables under linux. This article is based on RedHat 9.0 and explains the problem of environment variable customization in detail.

2. Introduction to Variables

Linux is a multi-user operating system. After each user logs in to the system, there will be a dedicated operating environment. Usually the default environment of each user is the same. This default environment is actually the definition of a set of environment variables. Users can customize their own operating environment by modifying the corresponding system environment variables.

3. Common environment variables

$ PATH: Determines in which directory the shell will look for commands or programs

$ HOME: current user home directory

$ MAIL: refers to the mail storage directory of the current user.

$ SHELL: Refers to what kind of Shell the current user is using.

$ HISTSIZE: refers to the number of records of historical commands saved

$ LOGNAME: refers to the login name of the current user.

$ HOSTNAME: refers to the name of the host, many applications If you want to use the host name, it is usually obtained from this environment variable.

$ LANG / LANGUGE: It is a language-related environment variable. Users who use multiple languages can modify this environment variable.

$ PS1: It is the basic prompt, which is # for the root user, and $ for the normal user. You can also use some more complex values.

$ PS2: It is an auxiliary prompt, the default is ">". You can modify the current command symbol by modifying this environment variable. For example, the following command will modify the prompt to the string "Hello, My NewPrompt :)"

# PS1 = "Hello, My NewPrompt :)"

$ IFS: Enter the field separator. When the shell reads input, a set of characters used to separate words. They are usually spaces, tabs, and newlines.

$ 0: The name of the shell script.

For example, in my Linux system:

$ Echo $ 0

/ Bin / bash

$ #: The number of parameters passed to the script.

$$: The process number of the shell script, which is usually used by script programs to generate a unique temporary file, such as / tmp / tmfile _ $$

For example, in my Linux system:

$ Echo $$

31038 #indicates that the current shell process number is 31038

4. Export command

The export command imports the variables as other parameters into the subshell and makes them effective in the subshell. The export command creates its own parameters as an environment variable, and this environment variable can be seen by other scripts and programs called by the current program.

4.1 Experiment Export variables

(1) We first list the script program export2

#。 / bin / sh

Echo "$ foo"

Echo "$ bar"

(2) Then the script export1. At the end of this script, we call export2:

#! / Bin / sh

Foo = "The first meta-syntactic variable"

Export bar = "The second meta-syntactic variable"

Export2

Run this script and you will get the following output:

$ Export1

#This is a space, because the variable foo is not available in export2, so $ foo is copied as empty

The second meta-syntactic variable

$

4.2 Set a new environment variable WELCOME

$ Export WELCOME = "Hello!"

$ Echo $ WELCOME

Hello!

 

5. Customize environment variables

Environment variables are closely related to the shell, a shell is started after the user logs in to the system. For Linux, it is generally bash, but it can also be reset or switched to another shell. Depending on the release version, bash has two basic system-level configuration files: / etc / bashrc and / etc / profile. These configuration files contain two different sets of variables: shell variables and environment variables. The former is only fixed in a specific shell (such as bash), the latter is fixed in a different shell. Obviously, shell variables are local, while environment variables are global. Environment variables are set by Shell commands, and the set environment variables can be used by all programs run by the current user. For the bash shell program, you can access the corresponding environment variables through variable names, and set the environment variables through export. Here are a few examples.

5.1 Use command echo to display environment variables

#This example uses echo to display the common variable HOME

$ Echo $ HOME

/ Home / lqm

5.2 Set a new environment variable

$ Export HELLO = "Hello!"

$ Echo $ HELLO

Hello!

     5.3 Use the env command to display all environment variables

$ Env

SSH_AGENT_PID = 1875

HOSTNAME = lqm

SHELL = / bin / bash

TERM = xterm

HISTSIZE = 1000

……

5.4 Use the set command to display all locally defined shell variables

$ Set

BASH = / bin / bash

……

     5.5 Use the unset command to clear environment variables

$ Export TEST = “test” # Add an environment variable TEST

$ Env | grep TEST # The output of this command proves that the environment variable TEST already exists

TEST = test

$ Unset $ TEST #Delete environment variable TEST

$ Env | grep TEST # No output from this command, which proves that the environment variable TEST already exists

5.6 Use the readonly command to set read-only variables

If the readonly command is used, the variables cannot be modified or cleared. Examples are as follows:

$ Export TEST = "Test" # Add an environment variable TEST

$ Readonly TEST #Set the environment variable TEST to read-only

$ Unset TEST #will find that this variable cannot be deleted

-Bash: unset: TEST: cannot unset: readonly variable

$ TEST = "New" #will find that this variable cannot be modified

-Bash: TEST: readonly variable

5.7 Use C program to access and set environment variables

For users of C programs, you can use the following three functions to set or access an environment variable.

Getenv () Access an environment variable. The input parameter is the name of the variable to be accessed, and the return value is a string. If the accessed environment variable does not exist, NULL will be returned.

Setenv () Function to set an environment variable in the program.

Unsetenv () Function to clear a specific environment variable.

In addition, there is a pointer variable environ which points to a list containing all environment variables. The following program can print out all environment variables in the current operating environment:

#Include <stdio.h>

Extern char ** environ;

Int main ()

{

Char ** var;

For (var = environ; * var! = NULL; ++ var)

Printf ("% s \ n", * var);

Return 0;

}

5.8 Modify the environment variable by modifying the environment variable definition file.

It should be noted that under normal circumstances, this is only applicable to ordinary users, avoid modifying the root user's environment definition file, because that may cause potential danger.

$ Vi / etc / bashrc #Modify shell variables

$ Vi / etc / profile #Modify environment variable definition file

Then edit your PATH statement, the format is:

PATH = $ PATH: <PATH 1>: <PATH 2>: <PATH 3>: ------: <PATH N>

You can add the specified path by yourself, separated by a colon. After the environment variable is changed, it will take effect the next time the user logs in. If you want to take effect immediately, you can execute the following statement: $ source .bash_profile

It should be noted that it is best not to put the current path "./" in the PATH, as this may be subject to unexpected attacks. After completion, you can view the current search path through $ echo $ PATH. After this customization, you can avoid frequently starting programs that are outside the path searched by the shell.


http://james23dier.iteye.com/blog/763274

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.