Linux Setup environment variable method (export PATH)

Source: Internet
Author: User
Tags echo command

1. Setting up a dynamic library path the Linux downgrade is not the same as Windows. Linux executables rely on the configuration file to read the path, so there are some times need to set the path specific operations as follows export ld_library_path=/home/... (Directory of dynamic Libraries) However, this setting method is only valid in the current session you can modify the configuration file to implement any session 2. Environment variable settings in general, you need to specify the path to the compilation tool when you configure the cross-compilation tool chain, and you need to set the environment variable. For example, my MIPS-LINUX-GCC compiler in the "/opt/au1200_rm/build_tools/bin" directory, Build_tools is my compilation tool, there are three ways to set the environment variables: 1, directly with the Export command: # Export path= $PATH:/opt/au1200_rm/build_tools/bin See if it is already set, available commands export view: [[email protected] bin]# Exportdeclare -X bash_env= "/ROOT/.BASHRC" Declare-x g_broken_filenames= "1" declare-x histsize= "$" 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 :d i=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 "Declare-x ssh_connection=" 10.3.37.152 2236 10.3.37.186 "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 is already set, and the path of the compiler that I want to add is already in path. 2, modify the profile file: #vi/etc/profile Add: Export path= "$PATH:/opt/au1200_rm/build_tools/bin" let the EnvironmentThe immediate effect of the variable requires execution of the following command: #source/etc/profile3. Modify the. bashrc file: # VI/ROOT/.BASHRC in the Add: Export path= "$PATH:/opt/au1200_rm/build_tools/bin" After the two methods generally need to re-logout system to take effect, Finally, you can test it with the echo command: # echo $PATH See if the output is already/my_new_path this path. -----------------------------------------------------------------------------------"/bin", "/sbin", "/usr/bin", "  /usr/sbin ","/usr/local/bin "and other paths are already in the system environment variables, if the executable file in these standard locations, in the terminal command line input the software executable file name and parameters (if required parameters), enter. If not in a standard location, the file name needs to be preceded by a full path. But it's too much trouble to run like this every time, a "once and for all" approach is to add this path to the environment variable. The command "path= $PATH: Path" can add this path to an environment variable, but exiting the command line will fail. This line needs to be added to the environment variable file in order to be permanently active.  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 valid for this user. "Path= $PATH: Path 1: Path 2: ...: path n", which means that the path to the executable file includes the path originally set, 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 find the corresponding executable file in these paths in turn and hand it over to the system core execution. The "$PATH" means that the path originally set is still valid, so be careful not to miss out.  Some software may also have an environment variable of type "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 path names in the UNIX class system environment variables are separated by colons, not semicolons.  In addition, the more loaded the software, the more environment variables, in order to avoid confusion, it is recommended that all statements are added at the end of the file, in the software installation order. The format is as follows (): # Software Name-version number path= $PATH: Path 1: Path 2: ...: path n OTHER environment variableVolume =$ Other environment variables: ...  In "Profile" and ". Bash_profile", "#" is a comment symbol that is written here without any effect except visual separation. Setup is complete, log off and log back in, and the settings take effect.  If you do not log off, executing these statements directly in the shell can also take effect, but the scope is limited to the shell that executes the statements. Once the relevant environment variable is in effect, you don't have to run into the software's executable directory. From:http://blog.csdn.net/kpgood/archive/2009/03/07/3965446.aspx use Linux more and more friends, under the Linux development is the need to configure environment variables, The following is an example of configuring a Java environment variable to describe three ways to configure environment variables. 1. Modify the/etc/profile file if your computer is only recommended for development use, because all user shells have the right to use these environment variables, the system may pose a security issue. (1) Open with a text editor/etc/profile (2) at the end of the profile file, add: Java_home=/usr/share/jdk1.5.0_05path= $JAVA _home/bin: $PATHCLASSPATH =.: $JAVA _home/lib/dt.jar: $JAVA _home/lib/tools.jarexport java_homeexport pathexport CLASSPATH (3) Re-login Note: A. You want to change the/USR/SHARE/JDK1.5.0_05JDK to your JDK installation directory B. Use the colon ":" to separate the path C. $PATH/$CLASSPATH/$JAVA _home is used to refer to the value of the original environment variable when setting the environment variable In particular, it is a common mistake to be careful not to overwrite the original value. D. Current directory in Classpath "." It is a common mistake to throw away the current directory. E. Export is exporting these three variables as global variables. F. The case must be strictly differentiated. 2. Modify the. bashrc file This method is more secure, it can control the use of these environment variable permissions to the user level, if you need to give a user permissions to use these environment variables, you only need to modify their personal user home directory of the. bashrc file is available. (1) Open the. bashrc file (2) in the user directory with a text editor. Add: Set java_home=/us at the end of the. bashrc fileR/share/jdk1.5.0_05export java_homeset path= $JAVA _home/bin: $PATHexport pathset classpath=.: $JAVA _home/lib/dt.jar:$ Java_home/lib/tools.jarexport CLASSPATH (3) re-login 3. Directly under the shell to set the variable is not in favor of using this method, because the shell, your settings are not valid, so this method is only temporary use, and later to use the time again to set up, more trouble. Simply execute the following command at the Shell Terminal: Export java_home=/usr/share/jdk1.5.0_05export path= $JAVA _home/bin: $PATHexport classpath=.: $JAVA _ Home/lib/dt.jar: $JAVA _home/lib/tools.jar under the Linux system, if you download and install the application, it is likely that you will be prompted with "command not found" when typing its name. If you go to the installation destination folder each time, it is too cumbersome to find the executable file to work on. This involves setting the environment variable path, and the path setting is also a component of customizing the environment variable under Linux. Based on RedHat 9.0, this paper explains in detail the problem of environment variable customization. 2. Introduction to Variables Linux is a multi-user operating system. After each user logs on to the system, there is a dedicated runtime environment. Typically, the default environment for each user is the same, and the default environment is actually the definition of a set of environment variables. Users can customize their own operating environment by modifying the appropriate system environment variables.  3. Common environment Variables $PATH: determines which directories the shell will look for commands or programs $HOME: The current user home directory $MAIL: Refers to the current user's mail storage directory.  $SHELL: Refers to what kind of SHELL is used by the current user.  $HISTSIZE: Refers to the number of entries saved by the history command $LOGNAME: Refers to the login name of the current user.  $HOSTNAME: Refers to the name of the host, which is usually obtained from this environment variable if a host name is used by many applications.  $LANG/languge: is a language-related environment variable that users of multiple languages can modify. $PS 1: Is the basic prompt, for the root user is #, for ordinary users is $, you can also use some more complexValue. $PS 2: Is a secondary prompt, the default is ">". You can modify the current command by modifying this environment variable, such as the following command to modify the prompt to a string "hello,my newprompt:)    ”。 # ps1= "Hello,my newprompt:) $IFS: Enter the domain delimiter.  When the shell reads input, a set of characters used to separate words, usually spaces, tabs, and line breaks.    The name of the $0:shell script.  For example, on my Linux system: $ echo $0/bin/bash $#: The number of arguments passed to the script. $$:shell the process number of the script, the script usually uses it to generate a unique temporary file, such as/tmp/tmfile_$$ for example, on my Linux system: $ echo $$ 31038 #表示当前shell进程号为3 1038 4. Export command The Export command imports the variable that is his argument into the child shell and makes it valid in the child shell.  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 Experiment Export Variables (1) Let's first list the script Export2 #! /bin/sh echo "$foo" echo "$bar" (2) and 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 var Iable "Export2 Run this script, you will get the following output: $ Export1 #这是个空格, because the variable foo is not available in Export2, the    $foo is copied to empty the second meta-syntactic variable $4.2 Set a new environment variable WELCOME $ export welcome= "hello!"  $ echo $WELCOME hello! 5, custom environment variable environment variables are closely related to the Shell, the user login systemThen a Shell is launched. It's usually bash for Linux, but it can be reset or switched to another Shell. Depending on the release version, Bash has two basic system-level profiles:/ETC/BASHRC and/etc/profile. These configuration files contain two different sets of variables: Shell variables and environment variables. The former is fixed in a particular shell (such as bash), which is fixed in a different shell. It is obvious that the shell variables are local, and the environment variables are global. Environment variables are set by shell commands, and set environment variables can be used by all current users running programs. For bash, the Shell program can use variable names to access the appropriate environment variables, and export to set environment variables.  This is illustrated by a few examples below.  5.1 Use the command echo to display environment variables #本例使用echo显示常见的变量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 the environment variable $ export test= "Test" # Add an environment variable TEST $ env | grep Test # This command has an output that proves that the environment variable TEST already exists test=test $ unset $TEST #删除环境变量TEST $ env | grep Test # This command has no output and proves that the environment variable TEST already exists 5.6 use the ReadOnly command to set a read-only variable if the readonly command is used, the variable cannot be modified or erased. Examples are as follows: $ export test= "TEST" # Add an environmentVariable test $ readonly test #将环境变量TEST设为只读 $ unset test #会发现此变量不能被删除-bash:unset:te  St:cannot unset:readonly variable $ test= "New" #会发现此变量不能被修改-bash:test:readonly variable 5.7 use C program to access and set environment variables for C  Users of the program can use the following three functions to set or access an environment variable. Getenv () accesses an environment variable. The input parameter is the name of the variable that needs to be accessed, and the return value is a string.  If the environment variable being accessed does not exist, NULL is returned.  Setenv () a function that sets an environment variable inside a program.  Unsetenv () a function that clears a particular environment variable. In addition, there is a pointer variable environ, which points to a list that contains all the 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 Modifies the environment variable by modifying the environment variable definition file.  It is important to note that, in general, this is only applicable to ordinary users and avoids modifying the root user's environment definition file, as that may pose a potential hazard. $vi/ETC/BASHRC #修改shell变量 $vi/etc/profile #修改环境变量定义文件 then edit your PATH declaration in the format: path= $PATH: <path 1>:< Path 2>:<path 3>:------: <path n> You can add the specified path yourself, separated by a colon. When the environment variable is changed, it takes effect the next time the user logs on, and if you want to take effect immediately, you can execute the following statement: $ source. bash_profile Note that it is best not to put the current path "./" in path, which maySubjected to unexpected attacks. When you are finished, you can view the current search path through the echo $PATH. With this customization, you can avoid frequent launches of programs that are outside the path of the shell search.

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

Linux Setup environment variable method (export PATH)

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.