Skip to content

学习日记

正在关注 Linux 、开源、IT 的个人博客。

  • LearnDiary 英文博客
  • MobileMate 开源项目
  • 学习日记小店服务
  • 留言板

交互式登录 shell 和交互式非登录 shell 的区别 ( login shell, non-login shell )

2010年03月04日 星期四 16时38分05秒
在学习LFS 6.6 的第四章第3节(4.3. Adding the LFS User: http://www.linuxfromscratch.org/lfs/view/6.6/chapter04/addinguser.html )时,里面提到登录 shell 和非登录 shell 的概念,不太清楚它们的准确区别,故在 Ubuntu 9.04 下实验了一下。

在Ubuntu 9.04下的试验步骤:
在 Ubuntu 9.04 下的 /etc/profile 中会调用 /etc/bash.bashrc ,所以注释掉其中调用的 /etc/bash.bashrc 后进行试验。

在/etc/profile中写入试验变量: export VAR_TEST=mdx
在/etc/bash.bashrc中写入试验变量: export BASHRC_TEST=bashrc
建立并在~mdx/.bash_profile写入试验变量: export MDXVAR=mdxhome
建立并在~mdx/.bashrc中写入试验变量: export MDXBASHRC=mdxbashrc

按CTRL+ALT+F1登录进入控制台(也可以在X环境的终端下执行 su - mdx 进入登录shell)执行env,存在变量:VAR_TEST=mdx(位于/etc/profile)和MDXVAR=mdxhome(位于~mdx/.bash_profile);
输入bash命令,进入非登录shell(用logout命令退不出来)执行env,存在上面所有4个试验变量;猜测/etc/profile和~mdx/.bash_profile是继承前一个登录shell的变量;

执行exit退出非登录shell状态;
执行logout退出登录shell状态;

用另一个帐号gg重新登录(或 su - gg)进入登录shell执行env,只有变量VAR_TEST=mdx(位于/etc/profile中)存在;
在gg帐号的家目录下创建或写入相应文件:
在 ~gg/.bash_profile中写入试验变量: export MDXVAR=gghome
在 ~gg/.bashrc 中写入试验变量: export MDXBASHRC=ggbashrc

执行logout退出gg帐号的登录shell状态;

用gg帐号重新登录进入登录shell执行env,存在VAR_TEST=mdx(位于/etc/profile中)和MDXVAR=gghome(位于~gg/.bash_profile中) ;
执行 su mdx 进入另一个帐号mdx的非登录shell,执行env,存在VAR_TEST=mdx(位于/etc/profile中,应是继承来的), MDXVAR=gghome(位于~gg/.bash_profile中,可见非登录shell不会读取家目录下的~mdx/.bash_profile,这里的MDXVAR环境变量值应是继承自上面gg的登录shell的) ,MDXBASHRC=mdxbashrc(位于 ~mdx/.bashrc,可见非登录shell读取了家目录下的 ~mdx/.bashrc), BASHRC_TEST=bashrc(位于/etc/bash.bashrc,是非登录shell新读取的,登录shell不会读取这个的);

执行exit退出mdx的非登录shell;

另开一个终端修改/etc/profile中的export VAR_TEST=mdx 为 export VAR_TEST=gg;
在前面mdx退出的终端再执行su mdx登录入非登录shell,执行env,发现VAR_TEST的值仍是mdx而不是已经修改过的gg,可见非登录shell确实是不会读取/etc/profile中的值,而是继承了它的登录父shell的位于/etc/profile中环境变量值;
执行exit退出mdx的非登录shell,再执行su - mdx 进入mdx的登录shell执行env,存在 VAR_TEST=gg(位于/etc/profile中,可见登录shell读取了/etc/profile的值),MDXVAR=mdxhome(位于~mdx/.bash_profile中,可见登录shell读取了家目录下的~mdx/.bash_profile文件)。

综上所述,确实如在Ubuntu 9.04下man bash中所言:
       When  bash is invoked as an interactive login shell, or as a non-inter-
       active shell with the --login option, it first reads and executes  com-
       mands  from  the file /etc/profile, if that file exists.  After reading
       that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
       in  that order, and reads and executes commands from the first one that
       exists and is readable.  The --noprofile option may be  used  when  the
       shell is started to inhibit this behavior.

       When  a  login  shell  exits, bash reads and executes commands from the
       file ~/.bash_logout, if it exists.

       When an interactive shell that is not a login shell  is  started,  bash
       reads  and  executes  commands  from /etc/bash.bashrc and ~/.bashrc, if
       these files exist.  This may be inhibited by using the  --norc  option.
       The  --rcfile  file option will force bash to read and execute commands
       from file instead of /etc/bash.bashrc and ~/.bashrc.

在登录shell里,确实只读取/etc/profile和~/.bash_profile, ~/.bash_login, and ~/.profile按顺序最先的一个,并执行其中的命令。除非被 --noprofile选项禁止了;
在非登录shell里,确实只读取/etc/bash.bashrc和~/.bashrc,为什么上面在非登录shell里面有登录shell才应有的环境变量值呢?有两个原因:
一、是在登录shell才读的配置文件里调用了非登录shell的配置文件,如Ubuntu 9.04会在/etc/profile中调用/etc/bash.bashrc,如下:
    if [ -f /etc/bash.bashrc ]; then
        . /etc/bash.bashrc
    fi
二、非登录shell作为登录shell的子shell,继承了作为父shell的登录shell的环境变量的值。

在Ubuntu 9.04的X环境下打开终端得到的是一个交互非登录shell,当执行bash时,读取/etc/bash.bashrc and ~/.bashrc。

注意,上面两种shell都是交互式shell,即:交互式的shell会在tty上从用户输入中读取命令. 另一方面, 这样的shell能在启动时读取启动文件, 显示一个提示符, 并默认激活作业控制. 也就是说, 用户可以与shell交互. (参见: 33.1. 交互与非交互式的交互与非交互式的shell和脚本: http://www.tsnc.edu.cn/default/tsnc_wgrj/doc/abs-3.9.1_cn/html/intandnonint.html ),非交互式的shell(如 Linux 系统启动时初始化脚本和启动脚本)有另外的特性这里没有提及;

另外,上面提到的子shell会继承父shell的环境变量。不过,shell程序里面除了环境变量还有与 bash 操作界面有关的变量(如:PS1)和自定义变量,而父shell的自定义变量是不会被它的子shell继承的(参见: export: 自訂變數轉成環境變數  http://linux.vbird.org/linux_basic/0320bash.php#export )!另外,与bash操作界面有关的变量也不会被继承,如:PS1等。我是这样测试的:修改/etc/bash.bashrc中PS1的值; . /etc/bash.bashrc 使其生效;重命名/etc/bash.bashrc,这样下一个非登录shell不会读到其中的值和执行其中的设定;执行bash命令进入非登录子shell,会发现提示符并没有继承父shell的值。假如要让子 bash shell 继承父 bash shell 的 PS1 值(即在不存在 /etc/bash.bashrc 等自动设置 PS1 的情况下让子 bash shell 的提示符与父 bash shell 相同),同样可以用 export 命令把父 bash shell 中的 PS1 转换为环境变量。

Author littlebatPosted on 三月 4, 2010Categories LinuxTags bash, interactive, LFS, Linux, login, non-login, shell, 交互式, 区别, 登录, 非登录

文章导航

Previous Previous post: LFS 学习日记(第3章:软件包和补丁)
Next Next post: 学习鸟哥的 Linux 私房菜(第十一章、认识与学习 BASH)

关于我

littlebat,以博思考,以博会友。
免责声明:除文中明确声明,所有文字皆为个人思考。本博本着分享与交流目的公之于众,请自行取舍,谢谢。
关注新浪微博:https://weibo.com/learndiary(中文)
关注 Twitter: https://twitter.com/learndiary(英文)
电子邮件: dashing.meng(AT)gmail(DOT)com 。

本地搜索

近期文章

  • 超强免费的Linux文件恢复工具R-Linux简介
  • Linux下畅玩原神5.2的三个要点
  • Deepin 23 踩坑记
  • Linux下误删文件后的紧急应对策略:避免写操作与及时关机
  • openEuler 搭建 vsftpd 服务器(FTP Over SSL、虚拟用户)

近期评论

  • ssuupv发表在《留言板》
  • littlebat发表在《留言板》
  • lllove404发表在《留言板》
  • Linux技术内容创作实战:从视频制作到文章生成的高效流程分享 - 学习日记发表在《开源项目生存现况:xz投毒事件引发的思考与GNU tar维护挑战》
  • 用 bsdtar 做 Linux 全系统迁移 - 学习日记发表在《bsdtar 归档程序在保留文件特殊属性上比 GNU tar 更全面和简便》

分类目录

  • Java (209)
  • LFS (43)
  • Linux (142)
  • Web (151)
  • Windows (3)
  • 开源 (260)
  • 思考 (15)
  • 数据库 (35)
  • 新闻 (3)
  • 未分类 (285)
  • 目标 (112)
  • 程序 (40)
  • 站务 (26)
  • 网摘 (46)
  • 网络 (3)
  • 育儿 (3)
  • 英语 (131)
  • 计算机 (288)
  • 设计 (79)

文章归档

  • 2024年十二月 (3)
  • 2024年十月 (1)
  • 2024年九月 (1)
  • 2024年七月 (1)
  • 2024年六月 (2)
  • 2024年五月 (4)
  • 2024年四月 (4)
  • 2024年三月 (8)
  • 2024年二月 (4)
  • 2024年一月 (3)
  • 2023年十二月 (1)
  • 2023年九月 (1)
  • 2023年七月 (2)
  • 2023年六月 (1)
  • 2023年四月 (2)
  • 2023年三月 (3)
  • 2022年十一月 (2)
  • 2022年十月 (1)
  • 2022年九月 (1)
  • 2022年八月 (2)
  • 2022年六月 (1)
  • 2022年五月 (1)
  • 2022年四月 (2)
  • 2022年三月 (1)
  • 2022年二月 (1)
  • 2022年一月 (3)
  • 2021年十二月 (4)
  • 2021年十一月 (9)
  • 2021年十月 (1)
  • 2021年八月 (2)
  • 2021年七月 (1)
  • 2021年四月 (1)
  • 2021年三月 (1)
  • 2021年二月 (1)
  • 2021年一月 (1)
  • 2016年五月 (1)
  • 2016年四月 (1)
  • 2016年三月 (4)
  • 2016年二月 (3)
  • 2015年三月 (1)
  • 2015年一月 (1)
  • 2014年十二月 (2)
  • 2014年八月 (1)
  • 2014年一月 (1)
  • 2013年十二月 (2)
  • 2013年十一月 (3)
  • 2013年六月 (2)
  • 2012年六月 (1)
  • 2012年四月 (1)
  • 2012年三月 (1)
  • 2011年十月 (6)
  • 2011年九月 (2)
  • 2011年五月 (1)
  • 2011年四月 (3)
  • 2011年三月 (6)
  • 2011年一月 (3)
  • 2010年十二月 (11)
  • 2010年十一月 (6)
  • 2010年十月 (8)
  • 2010年九月 (4)
  • 2010年八月 (1)
  • 2010年七月 (8)
  • 2010年六月 (14)
  • 2010年五月 (4)
  • 2010年三月 (6)
  • 2010年二月 (4)
  • 2010年一月 (6)
  • 2009年十二月 (5)
  • 2009年十一月 (1)
  • 2009年九月 (4)
  • 2009年八月 (2)
  • 2009年七月 (4)
  • 2009年六月 (3)
  • 2009年三月 (1)
  • 2008年十二月 (4)
  • 2008年十一月 (2)
  • 2008年十月 (2)
  • 2008年九月 (7)
  • 2008年八月 (1)
  • 2008年七月 (1)
  • 2008年四月 (2)
  • 2008年三月 (3)
  • 2008年二月 (2)
  • 2008年一月 (2)
  • 2007年十二月 (5)
  • 2007年十一月 (12)
  • 2007年十月 (25)
  • 2007年九月 (41)
  • 2007年八月 (17)
  • 2007年七月 (6)
  • 2007年六月 (12)
  • 2007年五月 (14)
  • 2007年四月 (10)
  • 2007年三月 (12)
  • 2007年二月 (7)
  • 2007年一月 (13)
  • 2006年十二月 (41)
  • 2006年十一月 (51)
  • 2006年十月 (59)
  • 2006年九月 (59)
  • 2006年八月 (48)
  • 2006年七月 (40)
  • 2006年六月 (51)
  • 2006年五月 (37)
  • 2006年四月 (74)
  • 2006年三月 (70)
  • 2006年二月 (52)
  • 2006年一月 (74)
  • 2005年十二月 (82)
  • 2005年十一月 (77)
  • 2005年十月 (29)
  • 2005年九月 (16)
  • 2005年八月 (34)
  • 2005年七月 (15)
  • 2005年六月 (16)
  • 2005年五月 (22)
  • 2005年四月 (15)
  • 2005年三月 (11)
  • 2005年二月 (13)
  • 2005年一月 (24)
  • 2004年十二月 (56)
  • 2004年十一月 (85)
  • 2004年十月 (99)
  • 2004年九月 (6)

标签

  • Advanced Bash-Scripting Guide (25)
  • bash (40)
  • blog (35)
  • CVS (14)
  • eclipse (17)
  • hibernate (16)
  • Java (61)
  • JSP (45)
  • LFS (46)
  • Linux (283)
  • MVC (47)
  • mysql (27)
  • rss (27)
  • struts (60)
  • ubuntu (35)
  • UML (34)
  • windows (17)
  • wordpress (18)
  • 健康 (17)
  • 创意 (59)
  • 励志 (16)
  • 学习日记 (371)
  • 工作日记 (22)
  • 成员 (22)
  • 手机 (13)
  • 数据库 (38)
  • 文字 (31)
  • 文摘 (16)
  • 文档 (21)
  • 日记 (14)
  • 未分类 (13)
  • 测试 (13)
  • 源码 (23)
  • 生活 (28)
  • 精读 (34)
  • 网页 (24)
  • 育儿 (157)
  • 英语 (139)
  • 计算机 (29)
  • 设计 (16)
  • 设计模式 (27)
  • 软件 (39)
  • 软件工程 (20)
  • 项目实施 (54)
  • 高级Bash脚本编程指南 (18)
  • LearnDiary 英文博客
  • MobileMate 开源项目
  • 学习日记小店服务
  • 留言板
学习日记 Proudly powered by WordPress
蜀ICP备2021012931号-1 川公网安备51092202000377号