定製Bash歷史命令

2021-08-25 10:38:16 字數 799 閱讀 7506

一位網友在:

中提到了:當前終端中的命令歷史是在終端退出時寫入檔案,多個終端間的命令執行歷史不能實時共享。

的問題,我也遇到這樣的問題,基於他的解決方法上,在.bash_rc中新增:

prompt_command="history -a;history -n;$prompt_command"

使用如下命令去掉所有的重複歷史命令條目:

tac /home/zkl/.bash_history |awk '!a[$0]++' |tac > /tmp/.bash_history && mv /tmp/.bash_history /home/zkl/.bash_history -f

這一條命令的python實現:

#!/usr/bin/env python import sys if len(sys.argv) > 1: filename = sys.argv[1] else: filename = "/home/zkl/.bash_history" fd = open(filename, "rb") lines = fd.readlines() line_dict = {} cnt = 1 for l in lines: line_dict[l] = cnt cnt += 1 fd.close() fd = open(filename, "wb") #fd.writelines(dict(sorted(line_dict.items(), key=lambda d:d[1])).keys()) for sl in sorted(line_dict.items(), key=lambda d:d[1]): fd.writelines(sl[0]) fd.close()

Bash的歷史命令

一 歷史命令 1 語法 history 選項 歷史命令儲存檔案 選項 c 清空歷史命令 w 把快取中的歷史命令寫入歷史命令儲存檔案 bash histoty 歷史命令預設儲存1000條,可以在環境變數配置檔案 etc profile中進行修改。2 歷史命令呼叫方式 3 命令與檔案補全 在bash中,...

bash歷史命令數 如何使用Bash歷史記錄命令

bash歷史命令數 bash歷史悠久。也就是說,它是乙個老祖先的外殼 bourne外殼 但是它還有乙個很棒的history命令,它根據其功能數量超過了所有其他外殼歷史介面。bash版本的history允許反向搜尋,快速呼叫,重寫歷史記錄等。history命令與許多其他命令不同。您可能習慣於將命令作為...

bash基礎特性之歷史命令

shell程序會在其會話中儲存此使用者提交執行過的命令,shell程序啟動後所有當前執行過的命令都會儲存在shell程序的記憶體中 只有使用者登出時才儲存到檔案中。1 history命令 用於檢視已經執行過的命令 1.root localhost history 2.1 ifconfig 3.2 s...