当我尝试设置我的终端窗口标题时:
$ echo -n "\033]0;foo\007"
\033]0;foo\007
它只是打印纯文本,终端标题没有变化。如何解决?
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.
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.