如何為每個新開啟的終端設定不同的shell提示顏色

如何為每個新開啟的終端設定不同的shell提示顏色

我使用的是 Ubuntu,可以手動將bashshell 提示顏色變更為綠色的使用

export PS1="\e[0;32m[\u@\h \W]\$ \e[m"

但是,我希望每當打開新終端或選項卡時 shell 提示顏色都會自動變更。我知道基本的 tty TERM 有 16 種顏色,如果打開的終端超過 16 個,可以旋轉顏色。當我透過Putty,tmux或進行連結時,該解決方案是否也有效screen

我的想法是編寫一個shell腳本並將其放置在.bashrc其中檢測用戶打開的新終端會話並將全域計數器從\e[0;31m[增加到\e[0;47m[。如何偵測使用者開啟的終端數量?

答案1

對終端進行計數(例如:who - a | grep user | wc -l)將無法運作:當一個或多個 shell 關閉時,總數會減少,並且新終端可能與另一個已開啟的終端相符。

你應該簡單地有一個計數器:

如果你想要總共 6 種顏色:

touch ~/.colornumber
new=$(awk '(NR==1) { print ($1 + 1) % 6; }' ~/.colornumber)
echo $new > ~/.colornumber
#and use color number $new for your current terminal, for example defined in an array of 6 entries. 
 # just note here that color 0 is for the 6th terminal, not the first... or change the awk above

相關內容