如何在 OS X El Capitan 中禁用 Bash 会话

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

How can I disable Bash sessions in OS X El Capitan

macosbashosx-elcapitan

提问by Whymarrh

A seemingly new feature in OS X El Capitan (10.11 Beta) is Bash sessions (Terminal sessions). I now have a ~/.bash_sessionsdirectory with history files, and my HISTFILEand HISTIGNOREenvars are being overridden. How can I disable all of this functionality?

OS X El Capitan (10.11 Beta) 中一个看似新的功能是 Bash 会话(终端会话)。我现在有一个~/.bash_sessions包含历史文件的目录,并且 myHISTFILEHISTIGNOREenvars 被覆盖。如何禁用所有这些功能?

采纳答案by Chris Page

This behavior is defined in /etc/bashrc_Apple_Terminal. It contains documentation comments describing what it does and how to customize it.

此行为在 中定义/etc/bashrc_Apple_Terminal。它包含描述它做什么以及如何定制它的文档注释。

You can disable the per-terminal-session command history feature by setting SHELL_SESSION_HISTORY=0in your ~/.bashrcscript, as described here:

您可以通过SHELL_SESSION_HISTORY=0~/.bashrc脚本中进行设置来禁用 per-terminal-session 命令历史记录功能,如下所述:

You may disable this behavior and share a single history by setting SHELL_SESSION_HISTORY to 0. There are some common user customizations that arrange to share new commands among running shells by manipulating the history at each prompt, and they typically include 'shopt -s histappend'; therefore, if the histappend shell option is enabled, per-session history is disabled by default. You may explicitly enable it by setting SHELL_SESSION_HISTORY to 1.

您可以通过将 SHELL_SESSION_HISTORY 设置为 0 来禁用此行为并共享单个历史记录。有一些常见的用户自定义设置通过在每个提示处操作历史记录来安排在运行的 shell 之间共享新命令,它们通常包括“shopt -s histappend”;因此,如果启用了 histappend shell 选项,则默认情况下会禁用每个会话的历史记录。您可以通过将 SHELL_SESSION_HISTORY 设置为 1 来显式启用它。

Note that, although you can disable the entire session-state restoration mechanism by creating ~/.bash_sessions_disable, this is unnecessary just to disable the per-session command history feature, and is not recommended.

请注意,虽然您可以通过 create 禁用整个会话状态恢复机制~/.bash_sessions_disable,但这并不是为了禁用每个会话的命令历史记录功能,因此不推荐这样做。

回答by Whymarrh

If you startup a new Bash session manually (i.e. bash -xl), you can see what is run on login.

如果您手动启动一个新的 Bash 会话(即bash -xl),您可以看到登录时运行的内容。

You'll see the following line in the output:

您将在输出中看到以下行:

....
+++ '[' '!' -e /Users/username/.bash_sessions_disable ']'

You can create a .bash_sessions_disablefile in your home directory to disable this functionality.

您可以.bash_sessions_disable在您的主目录中创建一个文件来禁用此功能。

回答by chetstone

To summarize, this has to do with Apple's Resumefeature. When this feature is enabled, when you quit an app or reboot, the next time the app opens it will open the windows that were previously open.

总而言之,这与 Apple 的Resume功能有关。启用此功能后,当您退出应用程序或重新启动时,应用程序下次打开时将打开之前打开的窗口。

With regard to Terminal, three things have to be in place for this to happen:

关于终端,必须做好三件事才能做到这一点:

  1. $HOME/.bash_sessions_disablemust notbe present
  2. System Preferences/General/Close Windows when quitting an appmust notbe checked.
  3. When rebooting, select Reopen windows.
  1. $HOME/.bash_sessions_disable必须存在
  2. System Preferences/General/Close Windows when quitting an app必须进行检查。
  3. 重新启动时,选择Reopen windows

When you reopen Terminal, it will reopen the same windows and tabs you had before in the same screen positions. Additionally, if you have checked Terminal/Preferences/your-window-type/Windows/Resume/Restore-text-when-reopening-windows, it will restore the text on each screen.

当您重新打开终端时,它将重新打开您之前在相同屏幕位置拥有的相同窗口和选项卡。此外,如果您已选中Terminal/Preferences/your-window-type/Windows/Resume/Restore-text-when-reopening-windows,它将恢复每个屏幕上的文本。

Finally, and this relates specifically to the OP's question, it will restore the history in each window/tab if the following conditions hold:

最后,这与 OP 的问题特别相关,如果满足以下条件,它将恢复每个窗口/选项卡中的历史记录:

  1. SHELL_SESSION_HISTORYis unset or set to 1
  2. shopt histappendis not set
  3. HISTTIMEFORMATis not set
  1. SHELL_SESSION_HISTORY未设置或设置为 1
  2. shopt histappend未设置
  3. HISTTIMEFORMAT未设置

If SHELL_SESSION_HISTORYis explicitly set to 1 in .bashrc, the last two requirements are overridden, that is, shopt histappendor HISTTIMEFORMATcould be set.

如果SHELL_SESSION_HISTORY显式设置为 1 in .bashrc,则最后两个要求将被覆盖,即shopt histappendHISTTIMEFORMAT可以设置。

Additionally, attention needs to be paid to the HISTSIZEand HISTFILESIZEvariables. They should not be too large or too small, and some advise to leave them unset so they take Apple's default values.

此外,需要注意HISTSIZEHISTFILESIZE变量。它们不应太大或太小,有些人建议不要设置它们,以便它们采用 Apple 的默认值。

回答by danemacmillan

Everything so far written about this subject is accurate and useful, and the techniques already mentioned should be used in lieu of this. I'm going to mention a totally nuclear approach, just for the sake of entertaining alternatives, and also to expand further understanding of this subject.

到目前为止,关于这个主题的所有文章都是准确和有用的,并且应该使用已经提到的技术来代替它。我将提到一种完全核方法,只是为了娱乐性的替代方案,并进一步扩大对这个主题的理解。

The only reason I acquired this knowledge is simply because I was looking for an alternative solution to needing to create a ~/.bash_sessions_disablefile; I would have instead preferred preventing the sessions behaviour from happening by just adding some lines to my existing ~/.bash_profile. Unfortunately, that is not possible without going nuclear, so the official answer is still the best approach.

我获得这些知识的唯一原因仅仅是因为我正在寻找需要创建~/.bash_sessions_disable文件的替代解决方案;相反,我宁愿通过向我现有的~/.bash_profile. 不幸的是,不去核是不可能的,所以官方的答案仍然是最好的方法。

Summary

概括

When Bash first starts up on MacOS, it will first source /etc/profile, which in turn sources /etc/bashrc. The contents of that file include this line:

当 Bash 首次在 MacOS 上启动时,它会首先/etc/profile获取/etc/bashrc. 该文件的内容包括这一行:

[ -r "/etc/bashrc_$TERM_PROGRAM" ] && . "/etc/bashrc_$TERM_PROGRAM"

The $TERM_PROGRAMenvironment variable is only set by Apple's Terminal app. Printing the value of that variable returns Apple_Terminal. In other words, the /etc/bashrcfile is attempting to source a /etc/bashrc_Apple_Terminalfile if it both exists and is readable. It's in this file where MacOS does its special Bash session handling to tie into the Resume features of the OS.

$TERM_PROGRAM环境变量只通过苹果的应用程序的终端设置。打印该变量的值返回Apple_Terminal。换句话说,如果文件既存在又可读,则该/etc/bashrc文件正在尝试获取/etc/bashrc_Apple_Terminal文件的源。正是在这个文件中,MacOS 执行其特殊的 Bash 会话处理以与操作系统的 Resume 功能联系起来。

After all that, Bash will then source any configurations a user has in their home directory (like ~/.bash_profileor ~/.bashrc). This being stated, there's no way to override all the work done in the /etc/bashrc_Apple_Terminalfile on a purely configuration level (vs both configuration andcreating a new file) without doing what the others have mentioned, namely, setting $SHELL_SESSION_HISTORYto 0to eliminate session-based history, and creating a ~/.bash_sessions_disableto prevent the .bash_sessionsdirectory from being created every time Apple's Terminal is started.

毕竟,Bash 将获取用户在其主目录中的任何配置(如~/.bash_profile~/.bashrc)。话虽如此,如果不执行其他人提到的操作,即设置为消除基于会话的历史记录,就无法/etc/bashrc_Apple_Terminal在纯粹的配置级别(与配置创建新文件相比)覆盖文件中完成的所有工作,并创建一个以防止每次启动Apple的终端时创建目录。$SHELL_SESSION_HISTORY0~/.bash_sessions_disable.bash_sessions

Nuclear Approach

核方法

The two possible alternatives to eliminate any of this new MacOS functionality would be to either 1) remove that last line from the /etc/bashrcfile, or 2) rename or delete the /etc/bashrc_Apple_Terminalto something else.

消除任何这种新的 MacOS 功能的两种可能的替代方法是:1) 从/etc/bashrc文件中删除最后一行,或 2) 重命名或删除/etc/bashrc_Apple_Terminal其他内容。

After doing this, Apple's Terminal app will no longer behave differently than non-Apple terminal emulators.

这样做之后,Apple 的终端应用程序的行为将不再与非 Apple 终端模拟器不同。