.bash_profile和.bashrc说明(转)_jiaxi

来源:百度文库 编辑:神马文学网 时间:2024/04/29 11:01:18
/etc/profile:
此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行。并从/etc/profile.d目录的配置文件中搜集shell的设置.
/etc/bashrc:
为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取.
~/.bash_profile:
每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件.
~/.bashrc:
该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该该文件被读取.
~/.bash_logout:
当每次退出系统(退出bash shell)时,执行该文件.
 
另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是"父子"关系.
~/.bash_profile 是交互式、login 方式进入 bash 运行的
~/.bashrc 是交互式 non-login 方式进入 bash 运行的
通常二者设置大致相同,所以通常前者会调用后者。
 

* 每次bash作为login shell启动时会执行.bash_profile。
主要有(我所知道的)有以下几种情形:
a) 每次登录到服务器时默认启动的shell
b) “su -l [USER]”时进入的shell
c) “bash --login”进入的shell
* 每次bash作为普通的交互shell(interactive shell)启动时会执行.bashrc
常见的有:
i) “su [USER]”进入的shell
ii) 直接运行“bash”命令进入的shell。
** 注意
1, 在shell脚本中“#!/usr/bin/bash”启动的bash并不执行.bashrc。因为这里的bash不是
interactive shell。
2, bash作为login shell(login bash)启动时并不执行.bashrc。虽然该shell也是interactive shell,
但它不是普通的shell。
* 一般.bash_profile里都会调用.bashrc
尽管login bash启动时不会自动执行.bashrc,惯例上会在.bash_profile中显式调用.bashrc。
,-------------------------------------
| if [ -f ~/.bashrc ]; then
|    . ~/.bashrc
| fi
`-------------------------------------
 
.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
BASH_ENV=$HOME/.bashrc
USERNAME="root"
export USERNAME BASH_ENV PATH