Bash

bash 腳本如何檢測對視窗標題轉義字元的支持?

  • April 1, 2016

每次我在 bash 中輸入命令時都會執行一個調試陷阱,該命令設置視窗標題以指示正在執行的命令。我省略了所有配置細節並將其歸結為:

export PS1="\[\e]0;$GENERATED_WINDOW_TITLE\a\]$GENERATED_PROMPT"

這工作得非常好,只有一個障礙:如果 bash shell 在不支持此功能的環境中執行,則 GENERATED_WINDOW_TITLE 會隨每個提示列印在螢幕上。每當我從非 X 終端執行 bash 時,都會發生這種情況。

bash 如何判斷是否支持此轉義序列?

我認為沒有 terminfo 功能。在實踐中,測試 的值TERM應該足夠好。這就是我在.bashrcand.zshrc中所做的,我不記得這是個問題。

case $TERM in
 (|color(|?))(([Ekx]|dt|(ai|n)x)term|rxvt|screen*)*)
   PS1=$'\e\]0;$GENERATED_WINDOW_TITLE\a'"$PS1"
esac

有一個 terminfo 條目(ab)用於此目的,並且已成為多個錯誤報告的主題,建議將其應用於各種終端描述。參考terminfo(5)

  has_status_line           hs     hs   has extra status
                                        line
  from_status_line          fsl    fs   return from status
                                        line
  to_status_line            tsl    ts   move to status line,
                                        column #1

這些在狀態行部分中進行了解釋:

一些帶有狀態行的終端需要特殊的序列來訪問狀態行。這些可以表示為帶有單個參數的字元串,**tsl**它將游標帶到狀態行上給定的零原點列。

順便說一句,您可能能夠使用的唯一支持該功能的終端仿真器是kterm.

延期是合適的。該screen程序記錄了一個可能的選擇(但在檢查了它對該功能的作用之後,這個想法被放棄了)。ncurses 提供了一個擴展,它已經在終端數據庫中使用了幾年,記錄在XTerm Extensions 部分

# TS is a string capability which acts like "tsl", but uses no parameter and
#    goes to the first column of the "status line".

最終,任何使用該功能的東西都將繼承自xterm+sl

# These building-blocks allow access to the X titlebar and icon name as a
# status line.  There are a few problems in using them in entries:
#
# a) tsl should have a parameter to denote the column on which to transfer to
#    the status line.
# b) the "0" code for xterm updates both icon-title and window title.  Some
#    window managers such as twm (and possibly window managers descended from
#    it such as tvtwm, ctwm, and vtwm) track windows by icon-name. Thus, you
#    don't want to mess with icon-name when using those window managers.
#
# The extension "TS" is preferable, because it does not accept a parameter.
# However, if you are using a non-extended terminfo, "TS" is not visible.

(ncurses)tput程序可以測試這個特性。

引用自:https://unix.stackexchange.com/questions/66226