Unix `bash` 命令在没有选项的情况下运行时究竟做了什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29125177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:33:35  来源:igfitidea点击:

What exactly does the Unix `bash` command do when run without options?

bashunixterminal

提问by Marco Prins

When you type bashinto the terminal and press enter, you go into what looks like an interactive bash interpreter... Which is as far as I know, what Terminal pretty much is, anyway.

当您bash在终端中输入并按 Enter 键时,您会进入一个看起来像交互式 bash 解释器的东西……据我所知,无论如何,终端几乎是什么。

The only visible difference is that the command prompt line says

唯一可见的区别是命令提示符行说

bash-3.2$

instead of

代替

Marcos-MacBook-Pro-3:Desktop marcoprins$

So what is happening when you run bashwithout options?

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

回答by Bryan Oakley

The short answer is that when you type "bash" at a bash prompt, it starts a new bash process.

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

Bash is a program that reads command and executes them. It can read them from a file, or you can type them from an interactive prompt.

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

When you run a terminal, it's simply a window that runs bash in interactive mode, possibly reading some initialization code first. When you type "bash" at one of these prompts it simply starts anotherinstance of the bash program (ie: another process), running "inside" the original bash program (process) running in the window. When you exit this new bash program, you will be returned to the original bash program where you can type more commands.

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

The prompt may or may not be different based on a whole bunch of reasons, many of which can be fine-tuned with bash command line options. Even if the prompt looks the same, you are operating in a different process from the original bash.

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

回答by baky

The INVOCATION section in the man pages is pretty clear.

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

Posting a section of it here:

在这里发布它的一部分:

   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.
   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.

回答by ComputerDruid

Bash runs your .bashrc when it is interactive (started with bash -i), which is true of the bash spawned by terminal emulators.

Bash 在交互式(以 开头bash -i)时运行您的 .bashrc ,这对于终端模拟器产生的 bash 来说是正确的。