简体   繁体   English

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

[英]How can I disable Bash sessions in OS X El Capitan

A seemingly new feature in OS X El Capitan (10.11 Beta) is Bash sessions (Terminal sessions) . OS X El Capitan (10.11 Beta) 中一个看似新的功能是 Bash 会话(终端会话) I now have a ~/.bash_sessions directory with history files, and my HISTFILE and HISTIGNORE envars are being overridden.我现在有一个包含历史文件的~/.bash_sessions目录,并且我的HISTFILEHISTIGNORE环境被覆盖。 How can I disable all of this functionality?如何禁用所有这些功能?

If you startup a new Bash session manually (ie 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_disable file in your home directory to disable this functionality.您可以在您的主目录中创建一个.bash_sessions_disable文件来禁用此功能。

This behavior is defined in /etc/bashrc_Apple_Terminal .此行为在/etc/bashrc_Apple_Terminal定义。 It contains documentation comments describing what it does and how to customize it.它包含描述它做什么以及如何定制它的文档注释。

You can disable the per-terminal-session command history feature by setting SHELL_SESSION_HISTORY=0 in your ~/.bashrc script, as described here:您可以通过在~/.bashrc脚本中设置SHELL_SESSION_HISTORY=0来禁用 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';您可以通过将 SHELL_SESSION_HISTORY 设置为 0 来禁用此行为并共享单个历史记录。有一些常见的用户自定义设置通过在每个提示处操作历史记录来安排在运行的 shell 之间共享新命令,它们通常包括“shopt -s histappend”; therefore, if the histappend shell option is enabled, per-session history is disabled by default.因此,如果启用了 histappend shell 选项,则默认情况下会禁用每个会话的历史记录。 You may explicitly enable it by setting SHELL_SESSION_HISTORY to 1.您可以通过将 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.请注意,虽然您可以通过创建~/.bash_sessions_disable来禁用整个会话状态恢复机制,但这~/.bash_sessions_disable为了禁用每个会话的命令历史记录功能,因此不推荐这样做。

To summarize, this has to do with Apple's Resume feature.总而言之,这与 Apple 的Resume功能有关。 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.启用此功能后,当您退出应用程序或重新启动时,应用程序下次打开时将打开之前打开的窗口。

With regard to Terminal, three things have to be in place for this to happen:关于终端,必须做好三件事才能做到这一点:

  1. $HOME/.bash_sessions_disable must not be present $HOME/.bash_sessions_disable不得存在
  2. System Preferences/General/Close Windows when quitting an app must not be checked. System Preferences/General/Close Windows when quitting an app不得选中System Preferences/General/Close Windows when quitting an app
  3. When rebooting, select Reopen windows .重新启动时,选择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_HISTORY is unset or set to 1 SHELL_SESSION_HISTORY未设置或设置为 1
  2. shopt histappend is not set shopt histappend未设置
  3. HISTTIMEFORMAT is not set未设置HISTTIMEFORMAT

If SHELL_SESSION_HISTORY is explicitly set to 1 in .bashrc , the last two requirements are overridden, that is, shopt histappend or HISTTIMEFORMAT could be set.如果在.bashrc SHELL_SESSION_HISTORY显式设置为 1,则最后两个要求将被覆盖,即可以设置shopt histappend HISTTIMEFORMATHISTTIMEFORMAT

Additionally, attention needs to be paid to the HISTSIZE and HISTFILESIZE variables.此外,需要注意HISTSIZEHISTFILESIZE变量。 They should not be too large or too small, and some advise to leave them unset so they take Apple's default values.它们不应太大或太小,有些人建议不要设置它们,以便它们采用 Apple 的默认值。

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_disable file;我获得这些知识的唯一原因仅仅是因为我正在寻找需要创建~/.bash_sessions_disable文件的替代解决方案; I would have instead preferred preventing the sessions behaviour from happening by just adding some lines to my existing ~/.bash_profile .我宁愿通过向我现有的~/.bash_profile添加一些行来阻止会话行为的发生。 Unfortunately, that is not possible without going nuclear, so the official answer is still the best approach.不幸的是,不去核是不可能的,所以官方的答案仍然是最好的方法。

Summary概括

When Bash first starts up on MacOS, it will first source /etc/profile , which in turn sources /etc/bashrc .当 Bash 首次在 MacOS 上启动时,它会首先获取/etc/profile ,然后获取/etc/bashrc The contents of that file include this line:该文件的内容包括这一行:

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

The $TERM_PROGRAM environment variable is only set by Apple's Terminal app. $TERM_PROGRAM环境变量仅由 Apple 的终端应用程序设置。 Printing the value of that variable returns Apple_Terminal .打印该变量的值会返回Apple_Terminal In other words, the /etc/bashrc file is attempting to source a /etc/bashrc_Apple_Terminal file if it both exists and is readable.换句话说, /etc/bashrc文件试图获取/etc/bashrc_Apple_Terminal文件(如果它既存在又可读)。 It's in this file where MacOS does its special Bash session handling to tie into the Resume features of the OS.正是在这个文件中,MacOS 执行其特殊的 Bash 会话处理以与操作系统的 Resume 功能联系起来。

After all that, Bash will then source any configurations a user has in their home directory (like ~/.bash_profile or ~/.bashrc ).毕竟,Bash 将获取用户在其主目录中的任何配置(如~/.bash_profile~/.bashrc )。 This being stated, there's no way to override all the work done in the /etc/bashrc_Apple_Terminal file on a purely configuration level (vs both configuration and creating a new file) without doing what the others have mentioned, namely, setting $SHELL_SESSION_HISTORY to 0 to eliminate session-based history, and creating a ~/.bash_sessions_disable to prevent the .bash_sessions directory from being created every time Apple's Terminal is started.话虽如此,如果不执行其他人提到的操作(即将$SHELL_SESSION_HISTORY设置为0 ),就无法在纯粹的配置级别(与配置创建新文件相比)覆盖/etc/bashrc_Apple_Terminal文件中完成的所有工作消除基于会话的历史记录,并创建一个~/.bash_sessions_disable以防止每次启动 Apple 终端时创建.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/bashrc file, or 2) rename or delete the /etc/bashrc_Apple_Terminal to 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 终端模拟器不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM