
Linux环境变量配置 在自定义安装软件的时候,经常需要配置环境变量,下面列举出各种对环境变量的配置方法。 下面所有例子的环境说明如下:
读取环境变量的方法:
这两个命令执行的效果如下 uusama@ubuntu:~$ exportdeclare -x HOME="/home/uusama"* O$ {% J" e- f$ Y' \ declare -x LANG="en_US.UTF-8"4 s5 C& k" }+ {! F$ Y$ V8 }6 M declare -x LANGUAGE="en_US:" declare -x LESSCLOSE="/usr/bin/lesspipe %s %s"1 _0 n$ g' j, A, u; r declare -x LESSOPEN="| /usr/bin/lesspipe %s" declare -x LOGNAME="uusama"' z: X( S0 [! B% T4 M# X declare -x MAIL="/var/mail/uusama" declare -x PATH="/home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" declare -x SSH_TTY="/dev/pts/0" declare -x TERM="xterm"6 b I4 g( a. `* W; c8 N6 E declare -x USER="uusama"4 ]+ _0 v# ?" S5 e) @4 d7 G uusama@ubuntu:~$ echo $PATH /home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 其中PATH变量定义了运行命令的查找路径,以冒号:分割不同的路径,使用export定义的时候可加双引号也可不加。 Linux环境变量配置方法一:export PATH使用export命令直接修改PATH的值,配置MySQL进入环境变量的方法: export PATH=/home/uusama/mysql/bin![]() #或者把PATH放在前面( ~9 q6 C- r, [" s9 g5 h; q export PATH=$PATH:/home/uusama/mysql/bin 注意事项:
通过修改用户目录下的~/.bashrc文件进行配置: vim ~/.bashrc# 在最后一行加上, `+ |0 g: r, C4 z j. I export PATH=$PATH:/home/uusama/mysql/bin 注意事项:
和修改~/.bashrc文件类似,也是要在文件最后加上新的路径即可: vim ~/.bash_profile# 在最后一行加上% k+ V2 l9 t: T/ v export PATH=$PATH:/home/uusama/mysql/bin 注意事项:
该方法是修改系统配置,需要管理员权限(如root)或者对该文件的写入权限: # 如果/etc/bashrc文件不可编辑,需要修改为可编辑' N+ `8 t; L; [$ g0 Kchmod -v u+w /etc/bashrc, e& a, P, l4 D A8 s8 r& W- K" C vim /etc/bashrc # 在最后一行加上 export PATH=$PATH:/home/uusama/mysql/bin 注意事项:
该方法修改系统配置,需要管理员权限或者对该文件的写入权限,和vim /etc/bashrc类似: # 如果/etc/profile文件不可编辑,需要修改为可编辑chmod -v u+w /etc/profile+ `$ g6 p+ v. j7 y& R vim /etc/profile: U" ?% n! D! X( p: Y7 k, w7 q m2 h# {3 C$ g, q5 f+ @# A* x # 在最后一行加上, b! x% c" m9 Z/ o. R export PATH=$PATH:/home/uusama/mysql/bin% D2 X# X, h- L0 H9 ~, } 注意事项:
该方法是修改系统环境配置文件,需要管理员权限或者对该文件的写入权限: # 如果/etc/bashrc文件不可编辑,需要修改为可编辑chmod -v u+w /etc/environment % `# C) F1 z. Q vim /etc/profile: U! i9 |8 J c # 在最后一行加上 export PATH=$PATH:/home/uusama/mysql/bin8 S: `' h. ]; y) |3 _% R 注意事项:
上面列出了环境变量的各种配置方法,那么Linux是如何加载这些配置的呢?是以什么样的顺序加载的呢? 特定的加载顺序会导致相同名称的环境变量定义被覆盖或者不生效。 环境变量的分类 环境变量可以简单的分成用户自定义的环境变量以及系统级别的环境变量。
另外在用户环境变量中,系统会首先读取~/.bash_profile(或者~/.profile)文件,如果没有该文件则读取~/.bash_login,根据这些文件中内容再去读取~/.bashrc。 测试Linux环境变量加载顺序的方法为了测试各个不同文件的环境变量加载顺序,我们在每个环境变量定义文件中的第一行都定义相同的环境变量UU_ORDER,该变量的值为本身的值连接上当前文件名称。 需要修改的文件如下: /etc/environment h3 y' R: J( E/etc/profile /etc/profile.d/test.sh,新建文件,没有文件夹可略过 /etc/bashrc,或者/etc/bash.bashrc! l9 q) \+ o( K2 } ~/.bash_profile,或者~/.profile+ t( r& A8 p- ^/ W ~/.bashrc 在每个文件中的第一行都加上下面这句代码,并相应的把冒号后的内容修改为当前文件的绝对文件名。 export UU_ORDER="$UU_ORDER:~/.bash_profile"修改完之后保存,新开一个窗口,然后echo $UU_ORDER观察变量的值: uusama@ubuntu:~$ echo $UU_ORDER$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc) o( @6 f9 a2 ]. ^2 b; Q 可以推测出Linux加载环境变量的顺序如下: /etc/environment/etc/profile- n1 l8 M7 t: d( _9 N) A5 n /etc/bash.bashrc /etc/profile.d/test.sh1 ` b( {; R0 V7 C/ R! t" R ~/.profile ~/.bashrc Linux环境变量文件加载详解 由上面的测试可容易得出Linux加载环境变量的顺序如下,: 系统环境变量 -> 用户自定义环境变量 /etc/environment -> /etc/profile -> ~/.profile 打开/etc/profile文件你会发现,该文件的代码中会加载/etc/bash.bashrc文件,然后检查/etc/profile.d/目录下的.sh文件并加载。 # /etc/profile: system-wide .profile file for the Bourne shell (sh(1))# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).+ G5 ~8 z. C3 D0 E if [ "$PS1" ]; then( |/ T" q9 Q7 N$ k( B if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then # The file bash.bashrc already sets the default PS1.% I' u0 F, O, g m# Q, I # PS1='\h:\w\$ '( r2 e. Y i" n# O5 T; ?5 S- ` o if [ -f /etc/bash.bashrc ]; then . /etc/bash.bashrc fi7 u# M! k# C, p5 p; W8 p) G else* u5 R+ y9 p6 d; L R g& h8 T if [ "`id -u`" -eq 0 ]; then6 y/ R) ^6 a7 U PS1='# ' else PS1='$ '/ Y/ n; M2 x+ V; b0 a) \ fi fi" o) E- k3 ~/ a3 X) q1 ` fi( o8 s. x) r9 c8 r/ m if [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do- F$ z, x3 W/ B7 I if [ -r $i ]; then; G: N. Y |" V( ? . $i# X6 V! c0 {+ i% E fi6 g; K# j( `! X$ x# P" c done unset i fi1 n8 a3 C8 {9 i4 G$ h# K7 O 其次再打开~/.profile文件,会发现该文件中加载了~/.bashrc文件。 # if running bashif [ -n "$BASH_VERSION" ]; then0 a4 a5 c; D+ T. @5 Y, t* m9 v # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc"! ]& N; t; {- n+ S( h fi fi # set PATH so it includes user's private bin directories PATH="$HOME/bin ![]() ![]() 从~/.profile文件中代码不难发现,/.profile文件只在用户登录的时候读取一次,而/.bashrc会在每次运行Shell脚本的时候读取一次。 一些小技巧可以自定义一个环境变量文件,比如在某个项目下定义uusama.profile,在这个文件中使用export定义一系列变量,然后在~/.profile文件后面加上:sourc uusama.profile,这样你每次登陆都可以在Shell脚本中使用自己定义的一系列变量。 也可以使用alias命令定义一些命令的别名,比如alias rm="rm -i"(双引号必须),并把这个代码加入到~/.profile中,这样你每次使用rm命令的时候,都相当于使用rm -i命令,非常方便。 # } L' m0 h6 H- z, N |