当您在终端中键入bash
并按回车键时,您将进入看起来像交互式bash解释器......这就是我所知道的,无论如何终端就是什么。
唯一可见的区别是命令提示行显示
bash-3.2$
而不是
Marcos-MacBook-Pro-3:Desktop marcoprins$
那么在没有选项的情况下运行bash
会发生什么?
答案 0 :(得分:4)
简短的回答是,当您在bash提示符下键入“bash”时,它会启动一个新的bash进程。
Bash是一个读取命令并执行它们的程序。它可以从文件中读取它们,也可以从交互式提示中键入它们。
当你运行终端时,它只是一个以交互模式运行bash的窗口,可能首先读取一些初始化代码。当您在其中一个提示符下键入“bash”时,它只是启动 bash程序的另一个实例(即:另一个进程),在窗口中运行的原始bash程序(进程)内部运行。当您退出这个新的bash程序时,您将返回到原始的bash程序,您可以在其中输入更多命令。
根据一系列原因,提示可能会有所不同,也可能没有不同,其中许多原因都可以通过bash命令行选项进行微调。即使提示看起来相同,您也会在与原始bash不同的过程中运行。
答案 1 :(得分:0)
Bash在交互时运行你的.bashrc(以bash -i
开头),这对终端模拟器产生的bash是正确的。
答案 2 :(得分:0)
手册页中的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.