2

当我尝试设置我的终端窗口标题时:

$ echo -n "\033]0;foo\007"
\033]0;foo\007

它只是打印纯文本,终端标题没有变化。如何解决?

4

2 回答 2

4

You're missing the -e to echo which tells it to interpret backslash escape sequences. Try this:

$ echo -en "\033]0;foo\007"

Although @chris-page is correct about -n being non-standard, the bash builtin echo and the system /bin/echo both support -n. However, the system echo does not support -e which is the real import feature when trying to send those escape codes to the terminal.

Also be aware that the system wide /etc/bashrc sets PROMPT_COMMAND to the function update_terminal_cwd, which is defined as:

update_terminal_cwd() {
    # Identify the directory using a "file:" scheme URL,
    # including the host name to disambiguate local vs.
    # remote connections. Percent-escape spaces.
    local SEARCH=' '
    local REPLACE='%20'
    local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
    printf '\e]7;%s\a' "$PWD_URL"
}

And apparently the operating system code 7 for Apple's Terminal.app sets a special icon link to the current working directory. You can right click on the folder icon in the title bar to open Finder at that location. This code also modifies the title bar title a bit by prepending the last component of the current working directory.

I've been investigating how Terminal.app's tab titles vs title bar title are set. And apparently they follow along with xterm's pretty well, where 'icon name' is the tab title, and 'window title' is the title bar title.

  • The title bar title is whatever you set with osc-2 (or osc-0). If the extra path component is set by osc-7 it will be prepended to the title bar title. And if any components are checked in Preferences->Settings->Window, they will be appended to the title bar title.
  • The tab title is whatever you set with osc-1 (or osc-0). If that is unset, it will be the contents of the Title box of Preferences->Settings->Window with the active process name appended. If the title is exactly Terminal (whether set by the text box in preferences or by osc-1) the tab title will just be the active process name.
于 2013-12-11T22:37:40.530 回答
2
于 2013-09-11T13:08:51.127 回答