bash的环境配置文件


1. login shell 和 non-login shell

login shell 和 non-login shell 区别在于登录 login。 login shell : 通过完整的登录流程, 举例:通过tty1-tty6 登录,需要输入用户的账号和密码,此时取得的 bash 就是 login shell。 non-login shell : 取得bash接口的方法不需要重复登陆,比如下面两种方法: 1, 以 X Window登录linux后, 再以 X 的图形界面启动终端机,此时这个终端没有再次输入用户名和密码,这个bash环境为 non-login shell 2, 在原bash下再次执行bash,同样没有输入用户名和密码,此时第二个bash,子进程,为non-login shell。
login shell 与 non-login shell 读取的配置文件不一样。

2. login shell

login shell 读取两个配置文件: 1. /etc/profile 2. ~/.bash_profile,或 ~/.bash_login, 或 ~/.profile。

2.1 /etc/profile

这个配置为用户整体的配置,最好不要修改这个文件,除非给所有用户设置整体环境。 这个文件设置的变量主要有: PATH : 跟据用户设置 USER: 用户名 HOSTNAME: 主机名 HISTSIZE : 历史命令条数 该文件会调用下面文件: /etc/inputrc : 主要设置bash 热键,[Tab] 有没有声音等。 /etc/profile.d/*.sh : 主要规定bash接口的颜色,语系等,如果要给所有用户设置配置变量时,在/etc/profile.d/目录下,建 .sh 文件。 /etc/sysconfig/i18n: 该文件由/etc/profile.d/lang.sh 文件调用, 设置bash默认使用哪种语系。

2.2 ~/.bash_profile

bash读取完整体环境变量/etc/profile 后会读取个人用户配置。主要有下面三个文件: 1. ~/.bash_profile 2. ~/.bash_login 3. ~/.profile 只会读取其中一个文件,顺序为上面的顺序,只要前面文件存在,后面则不再读取。
$ vim ~/.bash_profile 

# .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
unset USERNAME
该文件会读取 ~/.bashrc 文件。 source和. 为读入配置文件。
]$ source .bashrc 
]$ . .bashrc 

3. non-login shell

non-login shell 会读取 ~/.bashrc 查看该文件:
# .bashrc

# User specific aliases and functions

# Source global definitions
if [ -f /etc/bashrc ]; then 
    . /etc/bashrc
fi

alias rm='rm -i'
会读取 /etc/bashrc 该文件会设置: 跟据UID设置umask PS1: 提示符 调用/etc/profile.d/*.sh

4. 其他配置

/etc/man.config 配置手册的目录,man 命令 ~/.bash_history 记录bash的历史命令 ~/.bash_logout 注销bash时,希望系统做的一些处理。

相关内容