如何從 ZSH 腳本呼叫 History/fc

如何從 ZSH 腳本呼叫 History/fc

我正在嘗試為 GeekTool 編寫一個腳本,它將在螢幕上顯示我最後的 X (20) 個命令。

我可以顯示檔案或使用 shell 命令的輸出(首選)。

問題是,當我從腳本運行命令時,出現~/bin/update_history_file.sh:fc:5: no such event: 0錯誤。

我的腳本檔案是:

#!/bin/zsh --login
#history | tail -n 25 > /tmp/history.txt

export HISTFILE=~/.zsh_history 
fc -l -20 -1

fc如果我從 shell 運行該命令,則該命令有效;但是,如果我從腳本運行它,它就會失敗。我認為這與無法讀取歷史文件有關,但尚未確認這一點。

我無法直接使用該.zsh_history文件,因為我使用 ZSH 擴展歷史記錄並嵌入了元資料。

答案1

您應該添加一個fc -R強制讀取歷史文件,然後刪除它-1

#!/bin/zsh --login
#history | tail -n 25 > /tmp/history.txt

export HISTFILE=~/.zsh_history 
fc -R
fc -l -20

相關內容