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

标签 bash shell interactive-shell

什么是login shellinteractive shell,什么是.bash_profile.bashrc

最佳答案

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.

[ gnu bash manual ]

登录 shell 是您登录的 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

交互式 shell 是一种从标准输入(通常是终端)读取命令的工具。

例如,

  • 如果您使用 xterm 或终端仿真器(如 putty)登录到 bash,则 session 是登录 shell 也是交互式的。
  • 如果您随后键入 bash,那么您将进入一个交互式 shell,但它不是登录 shell。

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

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

当登录 bash shell 被调用时,/etc/profile 被获取(在当前环境中执行)。之后,检查三个文件是否存在。这些文件的检查按此顺序完成,运行第一个存在的文件。

  • /etc/profile
  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile

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

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

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

关于bash - 登录 shell 和交互式 shell 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18186929/

相关文章:

mongodb - MongoDB 有 shell 历史文件吗?

bash - 在 root 拥有的 shell 脚本中创建文件,无需 sudo

linux - 如何在 shell 中获取一年中的某一天?

javascript - 如何方便、安全地分发脚本?

linux - 如何在 UNIX 中替换空字符?

php - 我可以 "use"来自 PHP 交互式 Shell 中必需/包含文件的命名空间吗?

linux - 转换后将文件移动到其原始位置

shell - 使用powershell获取csv文件中的特定单元格值

linux - linux中如何从多个xml文件中提取多个标签值

python - vscode ipython 交互式内核启动和执行缓慢