How to set environment variables in Linux (export PATH)

1. Setting the dynamic library path

Calling the dynamic library under Linux is different from that of Windows. The Linux executable program relies on the configuration file to read the path, so sometimes the path needs to be set.
The specific operation is as follows

export LD_LIBRARY_PATH=/home/.....(directory of 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

Generally speaking, when configuring the cross-compilation toolchain, you need to specify the path of the compilation tool, and then you need to set the environment variable. 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、直接用export命令:
#export PATH=$PATH:/opt/au1200_rm/build_tools/bin
查看是否已经设好,可用命令export查看:
[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",
you can see that the environment variable has 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 it:
export PATH="$PATH:/opt/au1200_rm/build_tools/bin"

To make the environment variable take effect immediately, you need to execute the following command:

#source /etc/profile

3. Modify the .bashrc file:
# vi /root/.bashrc
add:
export PATH="$PATH:/opt/au1200_rm/build_tools/bin"

The latter two methods generally need to log out of the system to take effect. Finally, you can use the echo command to test:
# echo $PATH
to see if the path /my_new_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 file is in these several Standard location, enter the file name and parameters of the software executable file (if parameters are required) on the terminal command line, and press Enter.

  If not in a standard location, the filename needs to be prefixed with the full path. 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 it will be invalid after exiting this command line. To make it permanent, you need to add this line to the environment variable file. There are two optional files: "/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 User is valid.

  "PATH=$PATH:path1:path2:...:pathn" means that the path of the executable file includes the originally set path, and also includes all paths from "path1" to "pathn". When the user enters a string of characters and presses Enter, the shell will find the corresponding executable file in these paths in turn and hand it over to the system core for execution. The "$PATH" indicates that the original path is still valid, so be careful not to miss it. Some software may have environment variables other than "PATH" that need to be added, but the method is the same, and also need to pay attention to "$".

  Note that, unlike DOS/Window, pathnames 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 to add all statements at the end of the file and add them in the order of software installation.

  The format is as follows ():

  # software name-version number

  PATH=$PATH:path1:path2:...:pathn

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

  In "profile" and ".bash_profile", "#" is a comment symbol, which has no effect other than visual separation.

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

  After the relevant environment variables take effect, there is no need to go to the executable file directory of the software to operate.

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

There are more and more friends who use linux. The first thing to do in linux development is to configure environment variables. The following takes configuring java environment variables as an example to introduce three ways to configure environment variables.

 

1. Modify the /etc/profile file

This method is recommended if your computer is only used for development, since all users' shells have access to these environment variables, which may cause security problems for the system.

 

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

 

(2) Add at the end of the profile file:

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 colon ":" to separate paths under linux

 

c. $PATH / $CLASSPATH / $JAVA_HOME are used to refer to the value of the original environment variable. When setting the environment variable, special attention should be paid not to overwrite the original value. This is a common mistake.

 

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

 

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

 

f. Upper and lower case must be strictly differentiated.

 

2. Modify the .bashrc file  

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

 

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

 

(2) Add at the end of the .bashrc file:  

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 in the shell

It is not recommended to use this method, because if you change the shell, your settings will be invalid, so this method is only used temporarily, and it is more troublesome to reset it when you want to use it later.

 

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, if you download and install the application, it is very likely that a "command not found" message will appear when you type its name. If you go to the installation target folder every time, it will be too cumbersome to find the executable file to operate. This involves the setting of the environment variable PATH, and the setting of PATH is also an integral part of customizing the environment variable under linux. This article is based on RedHat 9.0 and explains in detail the problem of environment variable customization.

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, and 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 which directories the shell will look for commands or programs in

       $ LD_LIBRARY_PATH : dynamic libraries

  $HOME: current user home directory

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

  $SHELL: Indicates which Shell is used by the current user.

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

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

  $HOSTNAME: refers to the name of the host, if many applications 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: is the basic prompt, # for root users, $ for normal users, and some more complex values ​​can be used.

  $PS2: is a subsidiary prompt, the default is ">". You can modify the current command line by modifying this environment variable. For example, the following command will change the prompt to the string "Hello,My NewPrompt :) ".

    # PS1=" Hello,My NewPrompt :) "

  $IFS: Input field separator. A set of characters used to separate words when the shell reads input, 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 arguments passed to the script.

  $$: The process ID of the shell script, which is usually used by the script 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 variable as its argument into the subshell and makes it available in the subshell. The export command creates its own parameters as an environment variable that can be seen by other scripts and programs called by the current program.

  4.1 Experimental derived 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

  Running this script, 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 Shell, and a Shell is started after the user logs in to the system. For Linux it is usually bash, but it can also be reset or switched to other shells. Depending on the distribution, bash has two basic system-wide 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 (eg bash), the latter is fixed in different shells. Obviously, shell variables are local and environment variables are global. Environment variables are set through 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 the variable name, and set the environment variables through export. A few examples are given below.

  5.1 Use the command echo to display environment variables

  #This example uses echo to display the common variable HOME

  $ echo $HOME

  /home/lqm

  5.2 Setting 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 # This command has output, proving that the environment variable TEST already exists

  TEST=test

  $ unset $TEST #delete the environment variable TEST

  $ env | grep TEST # This command has no output, 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 variable cannot be modified or cleared. An example is as follows:

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

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

  $ unset TEST #You will find that this variable cannot be deleted

  -bash: unset: TEST: cannot unset: readonly variable

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

  -bash: TEST: readonly variable

  5.7 Accessing and Setting Environment Variables with C Programs

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

  getenv() accesses 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 is returned.

  setenv() A function to set an environment variable in a 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 running 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 environment variables by modifying the environment variable definition file.

  It should be noted that in general, this is only applicable to ordinary users, avoid modifying the environment definition file of the root user, because that may cause potential danger.

  $vi /etc/bashrc #Modify shell variables 

  $vi /etc/profile #Modify the environment variable definition file

  Then edit your PATH statement to have the format:

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

  You can add the specified path yourself, separated by a colon. After the environment variable is changed, it will take effect when the user logs in next time. 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, which may be subject to unexpected attacks. Once done, the current search path can be viewed via $ echo $PATH. With this customization, you can avoid frequently starting programs that are not in the paths searched by the shell.

 

http://kangyang.blog.51cto.com/471772/590840

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327061269&siteId=291194637