【问题标题】:What exactly does the Unix `bash` command do when run without options?Unix `bash` 命令在不带选项运行时究竟做了什么?
【发布时间】:2015-05-21 10:06:51
【问题描述】:

当您在终端中输入bash 并按下回车键时,您会进入一个看起来像交互式 bash 解释器的界面……据我所知,终端几乎就是这样。

唯一明显的区别是命令行提示符

bash-3.2$

而不是

Marcos-MacBook-Pro-3:Desktop marcoprins$

那么当您在没有选项的情况下运行bash 时会发生什么?

【问题讨论】:

  • 您正在子shell 中执行一个bash 实例。输入 exit 将带您回到主 shell。
  • 关闭除终端以外的所有应用程序,并且只打开一个终端窗口,在窗口中键入ps xc。这将打印出大量的东西。现在打开另一个终端窗口。在该窗口中输入ps xc。比较两个列表。发生了什么变化?
  • @eigenchris 从技术上讲,这不是一个子shell,当你使用括号时会发生这种情况
  • @ComputerDruid 不是同一个想法吗?您正在启动一个 shell,它是另一个 shell 的子进程。我认为这将构成一个“子shell”。

标签: bash unix terminal


【解决方案1】:

简短的回答是,当您在 bash 提示符下键入“bash”时,它会启动一个新的 bash 进程。

Bash 是一个读取命令并执行它们的程序。它可以从文件中读取它们,或者您可以从交互式提示中键入它们。

当您运行终端时,它只是一个以交互模式运行 bash 的窗口,可能会先读取一些初始化代码。当您在其中一个提示符处键入“bash”时,它只是启动 bash 程序的 另一个 实例(即:另一个进程),在窗口中运行的原始 bash 程序(进程)“内部”运行。当您退出这个新的 bash 程序时,您将返回到原来的 bash 程序,您可以在其中键入更多命令。

提示可能会也可能不会因一大堆原因而有所不同,其中许多原因可以使用 bash 命令行选项进行微调。即使提示看起来相同,但您是在与原始 bash 不同的进程中操作。

【讨论】:

  • 如何使用 bash 开启交互模式?你知道吗?我假设 bash 检测它是否在 tty 中运行,如果是,则自动打开交互模式?
【解决方案2】:

手册页中的 INVOCATION 部分非常清楚。

在此处发布其中的一部分:

   A login shell is one whose first character of argument zero is a -, or one started with the --login option.

  An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals  (as  determined
   by isatty(3)), or one started with the -i option.  PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.

  The  following paragraphs describe how bash executes its startup files.  If any of the files exist but cannot be read, bash reports an error.  Tildes are expanded in file
   names as described below under Tilde Expansion in the EXPANSION section.

  When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from  the  file  /etc/pro-
   file,  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 files ~/.bash_logout and /etc/bash.bash_logout, if the files exists.

  When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists.  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 ~/.bashrc.

  When  bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there,
   and uses the expanded value as the name of a file to read and execute.  Bash behaves as if the following command were executed:
          if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
   but the value of the PATH variable is not used to search for the file name.

【讨论】:

  • 否决将 OP 告知 RTFM(以及仅包含实际上并未解决问题的联机帮助页引用的答案)。只有当您已经知道它试图告诉您什么时,bash 手册页才有意义。它非常适合“该 shell 内置的参数到底是什么?”但如果您对基本概念不清楚,这将毫无帮助。
【解决方案3】:

Bash 在交互时运行您的 .bashrc(以 bash -i 开头),终端模拟器生成的 bash 也是如此。

【讨论】:

  • 不,不是bash -i。只需bash
猜你喜欢
  • 2012-07-10
  • 1970-01-01
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 2020-09-21
  • 2011-06-25
  • 1970-01-01
相关资源
最近更新 更多