當前位置: 首頁>>技術問答>>正文


Bash曆史處理與多個終端

, , , , ,

問題描述

我主要使用終結者,它通常打開3分割終端窗口。我也因為各種原因使用Gnome終端。我想知道在這種情況下如何處理bash曆史記錄,因為我運行history時有時會錯過以前發出的命令

例如,我的提示符顯示當前的bash曆史記錄行(\!),如果我用3個拆分的終端窗口啟動終結符,則在所有終端上獲得相同的曆史記錄行(比方說100)。哪些曆史將被保存?

在使用終結者後也啟動Gnome終端,無論在之前在終結者中發布的所有命令,我在啟動時都會獲得第100行

最佳解決思路

保存的bash會話是最近關閉的終端的會話。如果你想保存每個會話的命令,你可以使用here解釋的技巧。

export PROMPT_COMMAND='history -a' 

To quote the manpage: “If set, the value is executed as a command prior to issuing each primary prompt.”

So every time my command has finished, it appends the unwritten history item to ~/.bash_history before displaying the prompt (only $PS1) again.

So after putting that line in /etc/bash.bashrc I don’t have to find myself reinventing wheels or lose valuable seconds re-typing stuff just because I was lazy with my terminals.

無論如何,您需要考慮到來自不同會話的命令會混合在您的曆史記錄文件中,因此稍後將不會很直接地閱讀它。

也可以看看:

次佳解決思路

在多次讀取man bash後,我為每個shell使用單獨的曆史文件。我做了一個mkdir -m 0700 ~/.history然後添加

[[ -d ~/.history ]] || mkdir --mode=0700 ~/.history
[[ -d ~/.history ]] && chmod 0700 ~/.history
HISTFILE=~/.history/history.$$
# close any old history file by zeroing HISTFILESIZE  
HISTFILESIZE=0  
# then set HISTFILESIZE to a large value
HISTFILESIZE=4096  
HISTSIZE=4096  

到我的~/.bashrc。我時不時地記得du -sk .history並將其清理幹淨。很高興能為我保存我輸入的每條命令。

我剛剛使用了上麵的內容來查看我最近做了些什麽,比如cut -f1 "-d " .history/* | sort | uniq -c |sort -n -r |lesscut -f1-2 "-d " .history/* | sort | uniq -c |sort -n -r |less(在排序鏈中包含第一個參數,例如sudo mount)。

第三種解決思路

從所有終端顯示history

export PROMPT_COMMAND='history -a; history -r'添加到您的.bashrc文件。

來源:http://northernmost.org/blog/flush-bash_history-after-each-command /comment-page-1/index.html#comment-640


第四種思路

另請參見“keeping persistent history in bash”。它提示您將任何終端中輸入的所有命令發送到”persistent history”文件(與通常為常規.history執行的操作一起)。

參考資料

本文由Ubuntu問答整理, 博文地址: https://ubuntuqa.com/zh-tw/article/959.html,未經允許,請勿轉載。