在Linux上开发或者部署应用时,免不了要设置配置文件,比如安装JDK,需要为java可执行文件配置环境变量。 大多数时候我们不需要关注shell,但是当你执行"sudo su" 命令时,发现并不能同时把环境变量切换到root的环境;当你执行远程shell文件-"ssh who@host file.sh",发现不能加载环境配置文件时,那么你就要搞清楚bash的环境配置文件加载原理来搞定这些问题。 本文全部是基于CentOS系统写的,其它Linux发行版本可能略有差异。
bash allows two synonyms for .bash_profile: .bash_login, derived from the C shell’s file named .login, and .profile, derived from the Bourne shell and Korn shell files named .profile. Only one of these three is read when you log in. If .bash_profile doesn’t exist in your home directory, then bash will look for .bash_login. If that doesn’t exist it will look for .profile. One advantage of bash’s ability to look for either synonym is that you can retain your .profile if you have been using the Bourne shell. If you need to add bash-specific commands, you can put them in .bash_profile followed by the command source .profile. When you log in, all the bash-specific commands will be executed and bash will source .profile, executing the remaining commands. If you decide to switch to using the Bourne shell you don’t have to modify your existing files. A similar approach was intended for .bash_login and the C shell .login, but due to differences in the basic syntax of the shells, this is not a good idea.
翻译如下:
bash允许.bash_profile 有两个同义词:
当你登录shell的时候,这三个文件中仅有一个会被读取。如果.bash_profile在你的用户主目录(home)下不存在,那么bash将会查找.bash_login。如果.bash_login不存在,那么bash将会查找.profile。
bash能够查找任意一个同义文件这种能力的优势就是--如果你使用了Bourne shell,能够保留你的.profile。如果你需要添加具体bash的命令,你可以把它们放进.bash_profile文件,并且执行命令source .profile。 如果你想切换到Bourne shell,你不必修改你已存在文件。对于.bash_login和C shell .login也是相同的,但是由于shell的基础语法存在差异,这么做并不推荐。
根本原因是为了兼容各种shell。
读取环境配置文件之前,需要先区分login shell和non-login shell,因为这两种shell读取的配置文件不一样。
基本原理到此已经结束,如果你还意犹未尽,再继续看CentOS系统的/etc/profile文件和~/.bash_profile文件的内容:
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
# ksh workaround
EUID=`id -u`
UID=`id -ru`
fi
USER="`id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi
# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
pathmunge /sbin after
fi
HOSTNAME=`/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth
else
export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
umask 002
else
umask 022
fi
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null 2>&1
fi
fi
done
unset i
unset -f pathmunge
export GREP_OPTIONS=--color=auto
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
作者:Lucky_Micky 链接:https://www.jianshu.com/p/3c19279661a6 來源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
(adsbygoogle = window.adsbygoogle || []).push({});