How to set environment variables in Linux (export PATH)

Reprinted from http://hi.baidu.com/cuit_6015/blog/item/8d61edec8934feddb21cb14c.html

Establishing a cross-compilation environment is an important step in embedded development. Installing a cross-compilation environment is to set environment variables for the cross-compilation tool. Here is an example of installing a cross-compilation environment: First: install the working directory. Copy the decompressed cross-compilation toolchain arm-linux-gcc 3.4.1 version to /usr/local/arm/3.4.1/ Actually: set environment variables. #gedit /root/.bashrc Edit the /root/.bashrc file, in the last line export PATH=$PATH:/usr/local/arm/3.4.1/bin At the end: Re-login to the Redhat system (do not restart the machine, start-> logout) to make the above settings take effect, enter arm-linux-gcc -v on the command line to check whether the cross-compilation environment is successfully established. Analyze the contents of the /root/bashrc file: .bashrc: ······ if [ -f /etc/bashrc ]; then       . /etc/bashrc fi export PATH=$PATH:/usr/local/arm/3.4. The first three lines of 1/bin mean "if the file /etc/bashrc exists, then execute it". The last line is added by myself to modify the PATH variable. Here is the effect of setting the environment variable. Running arm-linux- will run the corresponding command in export PATH=$PATH:/usr/local/arm/3.4.1/bin. And every time I log in, the environment variable PATH is automatically modified to the value I need.

















Question 1: Use the export command to set environment variables in the Linux operating system: for example, after export HOME=/home j, HOME in the script represents /home. How does the arm-linux-gcc command in the shell match /usr/local/arm/3.4 arm-linux-gcc in .1/bin associated? Can't see their relationship directly in the statement? Answer: It is true that the correlation between HOME and /home is not directly visible in the statement as in the example. That's because the environment variable PATH is blinding you. Use $echo $PATH to view the value of the environment variable PATH (env view all environment variable values)

 

Add a little knowledge here: The usage of special symbols is explained through the following content. export A=/q/jing:aaa/cc/ld export B=.:/liheng/wang export A=/cd/cdr:$A Everyone pay attention to the red symbol: : means parallel meaning, for example, there are multiple A variable values , separated by the : symbol. . Indicates the current directory you are working on. For example the pap command will look for the B environment variable. Type the pap command in /home, the system will first look for the content of B in the /home directory (ie the current path), if not in the /liheng/wang directory. $ represents the value of the variable before this definition, for example, $A represents /q/jing:aaa/cc/ld. That is to say, the role of the A=/cd/cdr:/q/jing:aaa/cc/ld PATH environment variable is to determine which directories the shell will search for commands or programs. arm-linu-gcc is a command. export PATH=$PATH:/usr/local/arm/3.4.1/bin is not difficult to understand. It means that the previous value of PATH ($PATH) and the new environment variable value (/usr/local/arm/3.4.1/bin ) are side by side and separated by symbols. Because arm-Linux-gcc only exists under /usr/local/arm/3.4.1/bin, the path of arm-linux-gcc can be separated by symbols during execution as /usr/local/arm/3.4 .1/bin Question 2: Why is it set in the /root/bashrc file? Not setting it in /etc/bashrc or some other file?














回答:linux给大家提供了自动设置环境变量的方法,那就是修改 .bashrc 文件。一般说来,有两个文件可以提供这种“进入系统时自动设置”的功能,一个是 /etc/bashrc,另一个是 ~/.bashrc。其中/etc/bashrc是被每个用户执行的,而~/.bashrc只被当前用户执行。所以/etc/bashrc只有root用户能更改,而~/.bashrc是各个用户私有的文件。 "~"指的是用户的home目录。我自己用root帐号登录的,所以对我来所~/bashrc就是/root/bashrc。
     这样,各个用户有自己独立的".bashrc"文件,可以根据需要自行更改,而不会互相干扰。 /etc/bashrc文件则是共享的,整个Linux中只有一份。一般来说,用户的"~/.bashrc"开头都会调用/etc/bashrc,因此更改/etc/bashrc会影响到所有用户,这个文件一般是root用户管理的。

提问3:Linux系统中/etc/profile与/etc/bashfile文件作用的区别?
(1)/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. 并从/etc/profile.d目录的配置文件中搜集shell的设置。他是为用户定制系统定制环境变量。
(2)/etc/bashrc: 是为每一个bash shell的用户定制环境变量,当bash shell被打开时,该文件被读取。~/bashrc是为某个bash shell用户定制环境变量

 

============================================================================================

环境变量就是系统或软件设置的一些参数,用户环境变量就是用户登录系统后,都有自已专用的运行环境。在Windows系统中用户环境变量保存在用户家目录,Linux也是同样的。本文主要是讲解Linux常用的环境变量和环境变量的设置。

一、环境变量相关操作(只应用于当前,要永久保存需要写入到相关文件。)
#echo $PATH
显示PATH设置。
#env
显示当前用户变量。
#set
显示当前Shell变量。
#export
显示当前导出成用户变量的shell变量。
#a=abc
定义一个Shell变量。
#export a=abc
定义一个Shell变量,并导出成用户变量。
#unset a
清除环境变量
#readonly a
设置只读环境变量

二、常见的环境变量
PATH      决定了shell将到哪些目录中寻找命令或程序
HOME       当前用户主目录
HISTSIZE     历史记录数
LOGNAME     当前用户的登录名
HOSTNAME   指主机的名称
SHELL      前用户Shell类型
LANGUGE     语言相关的环境变量,多语言可以修改此环境变量
MAIL      当前用户的邮件存放目录
PS1       基本提示符,对于root用户是#,对于普通用户是$
PS2       附属提示符,默认是“>”

三、Linux环境变量设置文件
/etc/profile
全局用户,应用于所有的Shell。
/$HOME/.profile
当前用户,应用于所有的Shell。
/etc/bash_bashrc
全局用户,应用于Bash Shell。
~/.bashrc
局部当前,应用于Bash Sell。

四、其它
在用户目录下用ls -a,能看见很多“.”开头的隐藏文件。如果不是用户建立的,那么这些文件都是环境设置文件。有其它Shell的设置文件,也有软件设置的文件。比 如:.lftp,它就是lftp软件的设置文件。如果安装了x-window的话,菜单和桌面设置都在.local里面。这些大家用久了自然就知道了。

export set env区别baidu一下就知道了。

补充:开启启动Xwindow,有些Linux发行版不会加载SHELL环境变量,因为Xwindow有自已的会话设置。例如gnome,有~/.gnomerc 或 /etc/X11/Xsession.d/55gnome-session_gnomerc。

 

http://blog.sina.com.cn/s/blog_5420e00001017z66.html

Guess you like

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