为什么我们有登录、非登录、交互式和非交互式 bash shell?

The*_*mer 23 linux bash shell command-line

所以bash 手册页解释了什么是登录和交互式 shell:

登录 shell 是参数零的第一个字符是 - 或以 --login 选项开头的 shell。

交互式 shell 是一个没有非选项参数并且没有 -c 选项的启动,其标准输入和错误都连接到终端(由 isatty(3) 确定),或者一个以 -i 选项启动。如果 bash 是交互式的,则设置 PS1 并且 $- 包括 i,允许 shell 脚本或启动文件测试此状态。

我认为这意味着我们可以有 4 种不同类型的 shell:

  • 交互式登录外壳,
  • 非交互式登录外壳,
  • 交互式非登录外壳,
  • 非交互式非登录 shell

但是为什么我们首先要有交互式/非交互式和登录/非登录 shell?为什么品种多?如果我们只有一种壳,我们会失去什么?

此外,当试图通过运行确定我是否在登录 shell 中时echo $-,它输出:

himBH
Run Code Online (Sandbox Code Playgroud)

关于这些标志的解释这里,但是hHm没有解释。有没有描述所有这些标志的地方?

mpy*_*mpy 21

这些是我对不同“类型”shell 的想法——不幸的是,我没有从一开始就目睹 Un*x 的兴起(我认为这个概念在历史上已经发展到一个很好的范围),所以请批评一下。

  • 当我登录系统时(现在通过图形 X 登录),可能有一些任务应该运行一次,例如建立到某种服务器的连接,向我显示今天的待办事项列表,自动启动一些命令等。这每次打开新终端时都不应该发生。所以这是一组配置文件(/etc/profile~/.bash_login等等,请参考手册的准确名单)只能由源登录炮弹。
  • 因此,要关闭连接,杀死一些程序,~/.bash_logout登录shell 存在时运行备份脚本。
  • 所以,我在终端中使用的“普通”shell 不应该是登录shell,但应该从 中读取我的个人首选项~/.bashrc,因为我希望我的键绑定与 shell 交互——因此这是一个交互式的、非登录外壳。
  • 最后,但同样重要的是,当 bash 用于编写脚本时,这两个都不重要。bash应该尽可能快地启动,即不应该读取任何配置文件。这是一个非交互式、非登录的 shell

所以,我对你的问题的回答如果我们只有一种类型的外壳,我们会失去什么?一句话概括:“灵活性”。


你的第二个问题的答案很简单:

$-列出当前的选项集。这些可以通过命令行参数设置bash或通过set内置。所以你必须看手册中的两个地方:

  • OPTIONS 部分:

    -i        If the -i option is present, the shell is interactive.
    
    Run Code Online (Sandbox Code Playgroud)
  • SHELL BUILTIN COMMANDS部分,小节set

    -h      Remember the location of commands as they are looked up for execution.  This is enabled by default.
    -m      Monitor  mode.  Job control is enabled.  This option is on by default for interactive shells on systems that sup?
            port it (see JOB CONTROL above).  Background processes run in a separate process  group  and  a  line  containing
            their exit status is printed upon their completion.
    -B      The shell performs brace expansion (see Brace Expansion above).  This is on by default.
    -H      Enable !  style history substitution.  This option is on by default when the shell is interactive.
    
    Run Code Online (Sandbox Code Playgroud)