文章同步发表在博主网站朗度云,传输门:http://www.wolfbe.com/detail/201608/278.html

在Linux系统上,我们会看到类似于profile和bashrc的文件,以用户名wolfbe为例,讲解这些文件的位置及作用。对于某一个用户,一般存在以下的文件:
/etc/profile;
/etc/bashrc 或 /etc/bash.bashrc;
/home/wolfbe/.profile;
/home/wolfbe/.bashrc;
/home/wolfbe/.bash_profile;(这文件在某些系统不一定存在)
/home/wolfbe/.bash_login;(这些文件在某些系统不一定存在)
 
其实,上面的这些文件都是保存用户环境信息的配置文件,在shell启动时被shell读取。区别在于/etc/profile与/etc/bashrc保存的信息是全局的,作用于所有的用户,/home/username下的配置信息是针对某个用户的,只对某个用户有效。为了彻底弄清楚profile与bashrc的作用,我们需要了解shell的运行模式。
 
bash是linux系统的一种常用的shell,它有几种不同的运行模式:
login shell;
non-login shell;
interactive shell;
non-interactive shell;(非交互式,如执行shell脚本)
通过不同的方式可以进入不同的shell的运行模式,如下:
  • 登录系统时,获得的是一种交互式的login shell;
  • 在终端下使用--login参数调用bash时,获得的是交互式的login shell,如果不加--login参数,则获得的是交互式的non-login shell;
  • 在脚本中使用--login选项调用bash时,获得的是非交互式的login shell(如#! /bin/bash),如果不加--login选项,则获得的是非交互式的non-login shell;
  • 使用su -切换用户时,获得的是交互式的login shell,如果不加"-"获得的是交互式的non-login shell。
 
login shell与non-login shell的主要区别在于启动时读取不同的配置文件,从而导致环境不同。
  • 交互式login shell启动时首先读取/etc/profile文件信息,一般会在/etc/profile加载/etc/bashrc文件,然后依次查找~/.bash_profile、~/.bash_login、~/.profile配置文件,并且读取找到的第一个且可读的文件,最后再读取~/.bashrc文件;login shell在退出时会读取~/.bash_logout文件;
  • 交互式non-login shell启动时首先读取/etc/bashrc文件,然后读取~/.bashrc文件;
  • 非交互式login shell启动时首先读取/etc/profile文件信息,但不会加载/etc/bashrc文件,然后依次查找~/.bash_profile、~/.bash_login、~/.profile配置文件,并且读取找到的第一个且可读的文件,但也不会再加载~/.bashrc文件;login shell在退出时会读取~/.bash_logout文件;
  • 非交互式non-login shell启动时不会读取上面的配置文件,而是找到环境变量BASH_ENV,并且读取BASH_ENV中指定的文件的命令;
 
由此可以知道,bashrc仅在交互式shell启动时被读取,profile仅在login shell启动进被读取。
 
下面通过一些例子来验证上面的说法,首先在上面的配置文件中都加入打印信息的代码:
 #/etc/profile
echo "invoke /etc/profile" #/etc/bash.bashrc
echo "invoke /etc/bash.bashrc" #~/.bashrc
echo "invoke .bashrc" #~/.profile
echo "invoke .profile" #~/.bash_logout
echo "invoke bash_logout"
 
为了测试非交互式的shell,新建三个shell文件,分别为login.sh、nologin.sh、bash_env.sh,并且在.profile后面加入环境变量BASH_ENV:
#! /bin/bash --login
echo "invoke login.sh" #! /bin/bash
echo "invoke nologin.sh" #! /bin/bash
echo "invoke bash_env.sh" #.profile后加入BASH_ENV
export BASH_ENV=/home/wolfbe/bash_env.sh #立即生效
source ./.profile
由于本linux系统上没有.bash_profile和.bash_login文件,所有只测试上面的几个文件。
 
1、测试交互式login shell启动:
$bash --login

invoke /etc/bash.bashrc
invoke /etc/profile
invoke .bashrc
invoke .profile $exit logout
invoke bash_logout
加载bashrc的文件和profile 的文件,由于bashrc文件都是包含在profile文件里面,所以bashrc信息先于profile文件显示出来。
 
2、测试交互式non-login shell启动:

$bash

invoke /etc/bash.bashrc
invoke .bashrc $exit exit
仅仅加载bashrc的文件,没有profile 的文件。
 
3、测试非交互式login shell启动:
$./login.sh

invoke /etc/profile
invoke .profile
invoke login.sh
仅仅加载profile的文件,没有bashrc的文件。
 
4、测试非交互式nologin shell启动:

$./nologin.sh

invoke bash_env.sh
invoke nologin.sh
可见先执行了bash_env.sh文件,即环境变量BASH_ENV指定的文件。
 
 
 

Linux中profile与bashrc的作用的更多相关文章

  1. Linux中profile、bashrc、bash_profile之间的区别和联系

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置. 英文描述为: # /etc/pr ...

  2. (转) Linux中profile、bashrc、bash_profile之间的区别和联系

    原文地址:http://blog.csdn.net/chenchong08/article/details/7833242 /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登 ...

  3. 【转载】Linux中profile、bashrc、bash_profile之间的区别和联系

    如果你想对所有的使用bash的用户修改某个配置并在以后打开的bash都生效的话可以修改这个文件,修改这个文件不用重启,重新打开一个bash即可生效.~/.bash_profile:每个用户都可使用该文 ...

  4. 【转】Linux中profile、bashrc、bash_profile之间的区别和联系

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登陆时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置. 英文描述为: # /etc/pr ...

  5. Linux中profile和bashrc的区别

    profile主要设置系统环境参数(可类比为Windows的系统环境变量),如$PATH /etc/profile ~/.bash_profile bashrc主要用来设置bash命令,如命令别名,a ...

  6. Linux中PATH环境变量的作用和使用方法

    关于PATH的作用:PATH说简单点就是一个字符串变量,当输入命令的时候LINUX会去查找PATH里面记录的路径.比如在根目录/下可以输入命令ls,在/usr目录下也可以输入ls,但其实ls这个命令根 ...

  7. linux关于profile 、bashrc 、.bash_profile、.bashrc的区别

    linux关于profile .bashrc ..bash_profile..bashrc的区别 - /etc/profile /etc/bashrc ~/.bash_profile ~/.bashr ...

  8. linux中test与[ ]指令的作用

    linux中test与[ ]指令的作用: 在Linux中,test和[ ]功能是一样的,类似于c语言中的( ).不过Linux的test和[ ]是指令.在和if或者while联用时要用空格分开.

  9. Linux中的Wheel组的作用

    原文:http://www.360doc.com/content/11/0505/10/4644186_114496525.shtml Linux中的Wheel组的作用(用自己的话翻译的) (原文) ...

随机推荐

  1. 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)

    代码如下: public int Sum_Solution(int n) { int temp = n; boolean b = (temp>0)&&(temp += Sum_S ...

  2. android studio无法更新之解决方案

    当发现android studio有更新时,当然第一时间就想更新,可惜被墙了. 解决方案: 下载wallproxy,百度你懂的 在proxy.ini中最上面,找到ip和port 接着,在android ...

  3. NoSQL数据库的分布式算法&&memcache集群的实现

    NoSQL数据库的分布式算法  http://blog.nosqlfan.com/html/4139.html 一致性hash算法在memcache集群中的应用   http://alunblog.d ...

  4. selenium python 定位一组对象

    为什么定位一组对象? 定位一组对象的思想    在定位一组对象的过程中我们如何实现?以前的都是通过具体的对象定位,那么定位一组我们就需要通过css来定位   在单个定位对象中使用的是find_elem ...

  5. eclipse的scala环境搭建

    两种方法使eclipse安装scala环境(eclipse luna) 1.下载eclipse for scala IDE http://scala-ide.org/download/sdk.html ...

  6. linux安装问题

    java: cannot execute binary file问题 主要原因是 linux系统是32位的,jdk版本是64位的. 补充知识: 1.查看linux位数: #uname -a 如果有x8 ...

  7. iOS UIImage DownLoad图片的下载缓存全部在此

    iOS图片的下载缓存全部在此 分类: iOS编程 -- : 2075人阅读 评论() 收藏 举报 注意: 我的文章只写给自己看 ------------------------------------ ...

  8. 4. Android框架和工具之 android-async-http

    1. android-async-http   简介 主要有以下功能: (1)发送异步http请求,在匿名callback对象中处理response信息: (2)http请求发生在UI(主)线程之外的 ...

  9. IceFig阅读笔记

    嗯:就是这里了 http://research.worksap.com/research/icefig/ 一下阅读笔记: 嗯,时间有限,他们提供的又茫茫多,所以 就找出来了 几个 单独聊聊吧. 其他语 ...

  10. Java中String常用方法

    java中String的常用方法1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); int len= ...