Bash运行相关的配置文件,摘自man page
- /bin/bash
The bash executable - /etc/profile
The systemwide initialization file, executed for login shells - /etc/bash.bashrc
The systemwide per-interactive-shell startup file - /etc/bash.bash.logout
The systemwide login shell cleanup file, executed when a login shell exits - ~/.bash_profile
The personal initialization file, executed for login shells - ~/.bashrc
The individual per-interactive-shell startup file - ~/.bash_logout
The individual login shell cleanup file, executed when a login shell exits - ~/.inputrc
Individual readline initialization file
##/etc/profile
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置.
- profile 使用Bourne shell语法(兼容ksh,csh)等等(zsh?)。
- profile 中的内容是为所有用户登陆做初始化的。
例如ubuntu下/etc/profile的默认内容如下:
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "$PS1" ]; then
if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
# The default umask is now handled by pam_umask.
# See pam_umask(8) and /etc/login.defs.
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
`
前半段主要是加载/etc/bash.bashrc的内容,后半段则是加载bash的一些初始化脚本,例如bash补全设置。
##~/.bashrc和/etc/bashrc /etc/bashrc: 为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取。
- Debian默认没有/etc/bashrc,但是可以新建一个并设置alias。
- 有一个/etc/bash.bashrc/(命名真的好不科学),它的作用是初始化交互式shell相关,默认是配置PS1变量。
- ~/.bashrc为每个用户的bash配置文件,仅针对对应的用户有效。
- ~/.bashrc可以使用bash的专有语法(所以一些fuction定义和alias可以在里面设置)。
/etc/enviroment
/etc/environment是设置整个系统的环境 * 最典型的是ubuntu里的全局path设置
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
- Debian之中这个文件是空的,PATH属性在profile设置。
- 不清楚能否兼容Bourne shell,可以设置各种环境变量。
##执行顺序 /etc/environment -> /etc/profile -> /etc/bashrc -> ~/profile -> ~/.bashrc 后面设置的会覆盖前面的设置。
- 例如你在四个文件设置了不同的变量,最后生效的是~/.bashrc的那个 。
- 位于/etc下的几个文件个人感觉实际上起到了一个默认配置的作用。
##bashrc的一点配置原则 对于/etc/bashrc来说,个人感觉应该尽最小配置原则,只有确定所有用户都用的到的配置才写到里面。事实上Debian干脆默认就没有/etc/bashrc这个文件,每当新建一个用户的时候,都根据从/etc/skel/的模版文件生成一份用户的~/.bashrc和~/.profile。如果你把太多的配置都集中到/etc/bashrc中,某些特殊需要的用户就需要在自己的文件中覆盖这些选项,与上一种方法相比,增加了人工,得不偿失。