bash 登录 shell 和交互式 shell 之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18186929/
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-10 00:03:16  来源:igfitidea点击:

What are the differences between a login shell and interactive shell?

bashshellinteractive-shell

提问by caesar

What is a login shelland interactive shell, and what is a .bash_profileand .bashrc?

什么是login shelland interactive shell,什么是.bash_profileand .bashrc

回答by cdarke

An interactive shell is one started without non-option arguments, unless -s is specified, without specifying the -c option, and whose input and error output are both connected to terminals (as determined by isatty(3)), or one started with the -i option.

An interactive shell generally reads from and writes to a user's terminal.

交互式 shell 是一个没有非选项参数启动的,除非指定了 -s,没有指定 -c 选项,并且其输入和错误输出都连接到终端(由 isatty(3) 确定),或者一个以-i 选项。

交互式外壳通常从用户的终端读取和写入。

[gnu bash manual]

[ gnu bash 手册]

A login shellis a shell where you login. You can recognize a login shell from a ps -flisting, it will have a hyphen at the start of the program name, for example:

一个登录shell是你登录一个壳。您可以从ps -f列表中识别登录 shell ,它会在程序名称的开头有一个连字符,例如:

root      3561  3553  0 09:38 pts/0    00:00:00 -bash
qa        7327  3432  0 10:46 pts/1    00:00:00 -bash

An interactive shellis one which reads commands from it's standard-input, usually a terminal.

一个交互式壳是一个从它的标准输入,通常是一个终端读取命令。

For example, if you login to bash using an xterm or terminal emulator like putty, then the session is both a login shell and an interactive one. If you then type bashthen you enter an interactive shell, but it is not a login shell.

例如,如果您使用 xterm 或终端模拟器(如 )登录到 bash putty,则会话既是登录 shell,又是交互式 shell。如果您随后键入,bash则您将进入一个交互式 shell,但它不是登录 shell。

If a shell script (a file containing shell commands) is run, then it is neither a login shell nor an interactive one.

如果运行了一个 shell 脚本(一个包含 shell 命令的文件),那么它既不是登录 shell,也不是交互式 shell。

Start-up filesare highly tailorable in bash:

启动文件在 bash 中是高度可定制的:

When a login bash shell is invoked, then /etc/profileis sourced (executed in the current environment). After that, three files are checked for existence. The checks for these files are done in this order:

当登录 bash shell 被调用时,然后/etc/profile是来源(在当前环境中执行)。之后,检查三个文件是否存在。这些文件的检查按以下顺序完成:

if /etc/profileexists, source (run) it
if ~/.bash_profileexists, source (run) it
if ~/.bash_loginexists, source (run) it
if ~/.profileexists, source (run) it

如果/etc/profile存在,源(运行)它
如果~/.bash_profile存在,源(运行)它
如果~/.bash_login存在,源(运行)它
如果~/.profile存在,源(运行)它

Once a match is found, the other files are ignored, even if they exist. The /etc/bashrcfile might be used by both the ~/.bash_profileand the ~/.bashrcfiles. That would mean that the /etc/bashrcfile is sourced on all interactive invocations of bash, whether it is a login or non-login shell.

一旦找到匹配项,其他文件将被忽略,即使它们存在。该/etc/bashrc文件可能被~/.bash_profile~/.bashrc文件使用。这意味着该/etc/bashrc文件来源于 bash 的所有交互式调用,无论是登录还是非登录 shell。

So, the .bashrcfile is also run every time you request a new interactive shell. This does not include a shell script. Normally variables, aliases or functions are placed in this file.

因此,.bashrc每次请求新的交互式 shell时,该文件也会运行。这不包括 shell 脚本。通常变量、别名或函数放在这个文件中。

Bash shell scripts read a different file if suitably instructed. If the user defines (usually in their own .bash_profile) a variable BASH_ENVwhich contains a filename, scripts will read this. If this variable is not set (and exported) then bash scripts will not read any startup files.

如果有适当的指示,Bash shell 脚本会读取不同的文件。如果用户定义(通常在他们自己的.bash_profile)一个BASH_ENV包含文件名的变量,脚本将读取它。如果未设置(并导出)此变量,则 bash 脚本将不会读取任何启动文件。

回答by arkascha

Since you probably know what a "shell" is and are using it your question only targets the differencebetween login shell and everything else...

由于您可能知道什么是“shell”并且正在使用它,因此您的问题仅针对登录 shell 和其他所有内容之间的区别...

A login shell only differs from any other shell by the fact that one or more initial setup scripts (resources) are loaded on startup, typically named with "profile" in their name. in there basic settings are defined that are derived to subsequently opened shells (so they only need to be defined once).

登录 shell 与任何其他 shell 的区别仅在于一个或多个初始设置脚本(资源)在启动时加载,通常以“profile”命名。在那里定义了派生到随后打开的外壳的基本设置(因此它们只需要定义一次)。