交互式shell(Interactive Shells)

本文来自于:

https://tiswww.case.edu/php/chet/bash/bashref.html#Interactive-Shells

 

在linux系统中,我们打开terminal就是开启了一个Bash进程,也就是开启了一个shell,此时的shell就是interactive shell。

下图就开启了一个interactive shell。echo $-时包含i

什么是interactive shell? 

1. 启动shell时,指定了-i选项,或

2. 启动shell时,不存在non-option参数(即:全是 选项参数),不指定-c选项,且shell的stdin和stderr都连接到terminal(由isatty(3)所决定)

interactive shell一般都 读&写 用户的terminal

启动interactive shell时,可以使用-s调用选项(-s invocation option)来可以设置 位置参数(positionial parameter)

 

6.3.2 Is this Shell Interactive? 

如何确定shell是不是 interactive shell?

查看 特殊参数(special parameter) - 的值即可。如果-的值包含了i,则就是shell就是 interactive的;

另一种方式是检查PS1变量

1. interactive shell时,PS1是设置过的;

2. non-interactive shell时,PS1则是未设置的

脚本中,查看 Bash运行在interactive下吗?

case "$-" in
*i*)	echo This shell is interactive ;;
*)	echo This shell is not interactive ;;
esac

# 或者检查 PS1变量
if [ -z "$PS1" ]; then
        echo This shell is not interactive
else
        echo This shell is interactive
fi
# 直接检查下:
marvin@vm1:~/sf$ echo $-
himBHs         
marvin@vm1:~/sf$ 
# 直接检查下PS1:
marvin@vm1:~/sf$ 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\]\$
marvin@vm1:~/sf$ 

 

6.3.3 Interactive Shell Behavior 

Interactive shell独有的行为( 和 non-interactive shell 不同的行为)

1. Interactive shell 会 读&执行 Bash Startup Files

2. Job Control默认开启。Job Control开启时,Bash忽略掉 键盘生成的Job Control 信号

    1. sigttin

    2. sigttou

    3. sigtstp

3. 读取第一行命令前,Bash先 expand&display PS1;

   读取多行命令 的第二行及后续行 命令前,Bash先 expands&display PS2;

   Bash读取命令完成,但,尚未执行命令前,Bash先 expands&display PS0

4. 在打印出主提示符(primary prompt),即$PS1 前,Bash把 PROMPT_COMMAND变量的值 作为命令执行

5. 利用readline命令 从用户terminal中读取 命令

6. 读取命令时,当Bash从stdin中收到EOF,Bash不是立即退出,而是查看ignoreeof选项的值来设置-o

7. command history和history expansion是默认启用的。Bash把命令历史存储到 $HISTFILE 文件中

8. alias expansion默认是启用的

9. 不存在任何trap时,Bash忽略sigterm

10. 不存在任何trap时,sigint会被捕获并被处理。sigint会中断某些shell builtins

11. 如果开启了shell选项 huponexit,interactive&login shell在退出时,给所有job发送 sighup

12. -n调用选项被忽略,set -n没有任何作用

13. 取决于三个shell变量 MAIL, MAILPATH, MAILCHECK的值,Bash会周期性检查mail

14. set -u已被启用时,由于 引用unbound shell变量 而引起的expansion error 并不会使得shell退出

      也就是说,当set -u关闭时,引用unbound shell变量会导致shell直接退出

15. ${var:?word}时,由于 var未设置,或var为null 而引起的expansion error 并不会使得shell退出

16. shell builtins 所遭遇的 redirection error 并不会使得shell退出

17. 运行在POSIX mode时,返回error status的某个shell builtins 并不会使得shell退出

18. 运行失败的exec 并不会使得shell退出

19. 解析器语法错误 并不会使得shell退出

20. 对cd命令的目录参数 简单的拼写纠正 默认开启。shopt的cdspell选项控制着 cd命令的目录参数 简单的拼写纠正

21. shell会检查TMOUT变量的值,如果打印了PS1之后,在指定的秒数之内未读取命令,则shell就退出

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值