文档详情

profile与bashrc的深入分析.doc

发布:2017-12-10约1.1万字共11页下载文档
文本预览下载声明
/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 用户登录后加载profile和bashrc的流程如下: 1)/etc/profile--------/etc/profile.d/*.sh 2)$HOME/.bash_profile--------$HOME/.bashrc----------/etc/bashrc 说明: bash首先执行/etc/profile脚本,/etc/profile脚本先依次执行/etc/profile.d/*.sh 随后bash会执行用户主目录下的.bash_profile脚本,.bash_profile脚本会执行用户主目录下的.bashrc脚本, 而.bashrc脚本会执行/etc/bashrc脚本 至此,所有的环境变量和初始化设定都已经加载完成. bash随后调用terminfo和inputrc,完成终端属性和键盘映射的设定. 其中PATH这个变量特殊说明一下: 如果是超级用户登录,在没有执行/etc/profile之前,PATH已经设定了下面的路径: /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 如果是普通用户,PATH在/etc/profile执行之前设定了以下的路径: /usr/local/bin:/bin:/usr/
显示全部
相似文档