linux 非登录shell自动,Linux 交互/非交互/登录/非登录 shell 的区别

文章目录

一、交互shell 和 非交互shell

1、交互shell

2、非交互shell

3、区分方法

二、登录shell 和 非登录shell

1、登录shell

2、非登录shell

3、区分方法

三、四种 shell 在调用上的区别

bash

1、交互式的登录shell (bash –il test.sh)

2、非交互式的登录shell (bash –l test.sh)

3、交互式的非登录shell (bash –i test.sh)

4、非交互式的非登录shell (bash test.sh)

sh

1、交互式的登录shell (sh –il test.sh)

2、非交互式的登录shell (sh –l test.sh)

3、交互式的非登录shell (sh –i test.sh)

4、非交互式的非登录shell (sh test.sh)

一、交互shell 和 非交互shell

1、交互shell

交互式:顾名思义就是 shell 与用户存在交互,

用户登录后,在终端上输入命令,shell 立即执行用户提交的命令。

当用户退出后,shell 也终止了。

2、非交互shell

非交互式:即 shell 与用户不存在交互,而是以 shell script 的方式执行的。

shell 读取存放在文件中的命令, 并且执行它们。

当它读到文件的结尾 EOF,shell 也就终止了。

3、区分方法

可以通过打印 $- 变量的值,并查看其中的 i - interactive 选项来区分交互式与非交互式shell。

比如

:~> echo $-

himBH

有 i - interactive ,所以是 交互式 shell;反之则为非交互式。

二、登录shell 和 非登录shell

1、登录shell

登录 shell 是指需要用户名、密码登录后进入的 shell,或者通过 --login 选项生成的 shell 。

2、非登录shell

非登录 shell 是指不需要输入用户名和密码即可打开的 shell,

比如输入命令 bash或者sh 就能进入一个全新的非登录 shell,

在 Gnome 或 KDE 中打开一个 “terminal” 窗口,也是一个非登录 shell。

3、区分方法

如何区分登录 shell 和非登录 shell 呢,可以通过查看 $0 的值,登录 shell 返回 -bash,而非登录 shell 返回的是 bash 。

需要注意的是:

执行 exit 命令, 退出的 shell 可以是登录 或者 非登录 shell ;

执行 logout 命令,则只能退出登录 shell,不能退出非登录 shell 。

:~> sh

:~> logout

sh: logout: not login shell: use `exit'

三、四种 shell 在调用上的区别

交互/非交互/登录/非登录 shell 这些组合情况, 在脚本的调用方面有区别;而且对于 bash 与 sh 也存在差异;

以下分情况说明具体的调用情况(假如所执行脚本名为 test.sh )

bash

1、交互式的登录shell (bash –il test.sh)

载入的信息:

/etc/profile

~/.bash_profile( -> ~/.bashrc -> /etc/bashrc)

~/.bash_login

~/.profile

2、非交互式的登录shell (bash –l test.sh)

载入的信息:

/etc/profile

~/.bash_profile ( -> ~/.bashrc -> /etc/bashrc)

~/.bash_login

~/.profile

$BASH_ENV

3、交互式的非登录shell (bash –i test.sh)

载入的信息:

~/.bashrc ( -> /etc/bashrc)

4、非交互式的非登录shell (bash test.sh)

载入的信息:

$BASH_ENV

sh

1、交互式的登录shell (sh –il test.sh)

载入的信息:

/etc/profile

~/.profile

2、非交互式的登录shell (sh –l test.sh)

载入的信息:

/etc/profile

~/.profile

3、交互式的非登录shell (sh –i test.sh)

载入的信息:

$ENV

4、非交互式的非登录shell (sh test.sh)

载入的信息:

nothing

综上可知,

交互/非交互/登录/非登录,这四种 shell 主要区别在于:是否载入相关配置文件!

这些配置的载入与否,导致了 Linux 很多默认选项的差异。

参考文档: Difference between Login Shell and Non-Login Shell?

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值