当前位置: 首页 > news >正文

Linux:bash在被调用时会读取哪些启动文件?

(本文基于5.1-6ubuntu1.1版本的bash)

bash在被调用时会读取哪些启动文件?要回答这个问题,首先要弄清楚两个概念:login shell和interactive shell。

login shell

login shell是指这样的shell:

  1. 第一个命令行参数(进程名)以-开头,也就是满足argv[0][0] == '-'
  2. 或者 设置了--login/-l选项

对于条件1,有一个例子:当执行su - [USER_NAME]时,su命令会启动一个login shell,argv[0][0]会被设置为-su手册中写到:

-, -l, --loginStart the shell as a login shell with an environment similar to a real login:•   clears all the environment variables except TERM and variables specified by --whitelist-environment•   initializes the environment variables HOME, SHELL, USER, LOGNAME, and PATH•   changes to the target user’s home directory•   sets argv[0] of the shell to '-' in order to make the shell a login shell

不满足login shell条件的即为non-login shell。

interactive shell

interactive shell是指这样的shell:

  1. 满足以下条件
    1. 没有非选项参数(如果设置了-s选项则可以有非选项参数(non-option arguments))
    2. 并且 没有设置-c选项
    3. 并且 标准输入和标准错误都被连接到终端
  2. 或者 设置了-i选项

对于一个interactive shell,PS1环境变量会被设置,同时$-中会含有i选项:

$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
$ echo $-
himBHs

interactive shell,顾名思义,指可交互的shell。所以像是创建子进程执行一个脚本(bash ./foo.sh)或是执行一条命令(bash -c 'ls')时,创建出来的bash进程就显然不是可交互的,因此不属于interactive shell。除此之外,bash还把重定向了标准输入或者标准错误的bash排除在了interactive shell之外。所以对于bash 2>/dev/null这种情况,尽管实际上是可交互的,但仍然不属于interactive shell。下面这些命令创建的bash进程都不属于interactive shell:

$ bash ./foo.sh
$ bash -c 'ls'
$ bash < foo.sh
$ bash 2>/dev/null

然而,bash提供了一个选项-i,可以强行将一个bash设置为interactive shell,上面提供的4个例子中,只要加上了-i选项,它们就全部变成了interactive shell。

所以说,判断一个bash是否为interactive shell,不能从表现上看,还是要看它在被调用时设置了哪些参数。

在代码层面上,interactive shell的判断逻辑如下:

  /* First, let the outside world know about our interactive status.A shell is interactive if the `-i' flag was given, or if all ofthe following conditions are met:no -c commandno arguments remaining or the -s flag givenstandard input is a terminalstandard error is a terminalRefer to Posix.2, the description of the `sh' utility. */if (forced_interactive ||		/* -i flag */(!command_execution_string &&	/* No -c command and ... */wordexp_only == 0 &&		/* No --wordexp and ... */((arg_index == argc) ||		/*   no remaining args or... */read_from_stdin) &&		/*   -s flag with args, and */isatty (fileno (stdin)) &&	/* Input is a terminal and */isatty (fileno (stderr))))	/* error output is a terminal. */init_interactive ();elseinit_noninteractive ();

不满足interactive shell条件的即为non-interactive shell。

不同情况下读取的启动文件

在了解了login shell和interactive shell的概念后,我们就可以看看在不同情况下bash会读取哪些启动文件了。

  1. login && (interactive || non-interactive) shell
    1. 读取/etc/profile
    2. 读取~/.bash_profile~/.bash_login~/.profile中存在且可读的第一个
  2. non-login && interactive shell
    1. 读取/etc/bash.bashrc
    2. 读取~/.bashrc
  3. non-login && non-interactive shell
    1. 读取BASH_ENV环境变量中保存的文件路径,如果存在的话

需要注意的是,尽管login shell不会直接读取/etc/bash.bashrc或是~/.bashrc,但是通常会在满足条件时,通过/etc/profile~/.bash_profile/~/.bash_login/~/.profile间接读取这两个文件:

# /etc/profileif [ "${PS1-}" ]; thenif [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then# The file bash.bashrc already sets the default PS1.# PS1='\h:\w\$ 'if [ -f /etc/bash.bashrc ]; then. /etc/bash.bashrcfielseif [ "$(id -u)" -eq 0 ]; thenPS1='# 'elsePS1='$ 'fifi
fi
# ~/.profileif [ -n "$BASH_VERSION" ]; then# include .bashrc if it existsif [ -f "$HOME/.bashrc" ]; then. "$HOME/.bashrc"fi
fi

特殊情况下读取的启动文件

sh的名字调用时

在这种情况下,bash会模仿sh的行为:

  1. login && (interactive || non-interactive) shell
    1. 读取/etc/profile
    2. 读取~/.profile
  2. non-login && interactive shell
    1. 读取ENV环境变量中保存的文件路径
  3. non-login && non-interactive shell
    1. 不读取启动文件

posix模式调用时

如果bash调用时设置了--posix参数,那么在interactive shell的情况下,bash会尝试从ENV环境变量中获取启动文件路径,不会读取其他启动文件。

ssh调用时

在被ssh调用时,或者标准输入是一个网络连接时,会触发一些特殊逻辑:在这种情况下,即使是non-login && non-interactive shell情况的组合,bash仍然会读取/etc/bash.bashrc~/.bashrc

  /* get the rshd/sshd case out of the way first. */if (interactive_shell == 0 && no_rc == 0 && login_shell == 0 &&act_like_sh == 0 && command_execution_string){run_by_ssh = (find_variable ("SSH_CLIENT") != (SHELL_VAR *)0) ||(find_variable ("SSH2_CLIENT") != (SHELL_VAR *)0);/* If we were run by sshd or we think we were run by rshd, execute~/.bashrc if we are a top-level shell. */if ((run_by_ssh || isnetconn (fileno (stdin))) && shell_level < 2){maybe_execute_file (SYS_BASHRC, 1);maybe_execute_file (bashrc_file, 1);return;}}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • SQL labs-SQL注入(三,sqlmap使用)
  • 实习心得list
  • Vue 3 实现左侧列表点击跳转滚动到右侧对应区域的功能
  • 高速板开源项目学习(二)
  • 【Node.js基础05】包的理解与使用
  • BUUCTF [MRCTF2020]Ezpop
  • LLM 大语言模型显存消耗估计与计算
  • C语言 —— 函数
  • 如何在Linux上安装配置RabbitMQ消息队列
  • json的数据结构
  • 测试用例的设计方法
  • AOP面向切面编程和log4j的使用(Java版)
  • 多列传感器数据多项式拟合python脚本
  • 斯坦福UE4 C++课学习补充 14:UMG-优化血量条
  • spring常用注解有哪些
  • [原]深入对比数据科学工具箱:Python和R 非结构化数据的结构化
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • angular2 简述
  • express + mock 让前后台并行开发
  • Git 使用集
  • Java 9 被无情抛弃,Java 8 直接升级到 Java 10!!
  • java第三方包学习之lombok
  • Redis 懒删除(lazy free)简史
  • Yii源码解读-服务定位器(Service Locator)
  • 第十八天-企业应用架构模式-基本模式
  • 多线程 start 和 run 方法到底有什么区别?
  • 技术胖1-4季视频复习— (看视频笔记)
  • 老板让我十分钟上手nx-admin
  • 力扣(LeetCode)357
  • 聊聊redis的数据结构的应用
  • 自定义函数
  • raise 与 raise ... from 的区别
  • Unity3D - 异步加载游戏场景与异步加载游戏资源进度条 ...
  • ​Kaggle X光肺炎检测比赛第二名方案解析 | CVPR 2020 Workshop
  • ​软考-高级-信息系统项目管理师教程 第四版【第23章-组织通用管理-思维导图】​
  • #、%和$符号在OGNL表达式中经常出现
  • (7) cmake 编译C++程序(二)
  • (BFS)hdoj2377-Bus Pass
  • (附源码)python房屋租赁管理系统 毕业设计 745613
  • (附源码)spring boot北京冬奥会志愿者报名系统 毕业设计 150947
  • (免费分享)基于springboot,vue疗养中心管理系统
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (算法)Travel Information Center
  • (转)Android学习笔记 --- android任务栈和启动模式
  • (转)Oracle存储过程编写经验和优化措施
  • (转)视频码率,帧率和分辨率的联系与区别
  • (转)真正的中国天气api接口xml,json(求加精) ...
  • ***通过什么方式***网吧
  • .net 调用海康SDK以及常见的坑解释
  • .Net语言中的StringBuilder:入门到精通
  • :O)修改linux硬件时间
  • @FeignClient注解,fallback和fallbackFactory
  • @vue/cli 3.x+引入jQuery
  • @zabbix数据库历史与趋势数据占用优化(mysql存储查询)
  • []C/C++读取串口接收到的数据程序