简体   繁体   中英

Terminal escape sequences not working on OS X Mavericks 10.9DP7

When i try to set my terminal window title:

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

It just print plain text, the terminal title had no change. How to fix it?

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.

Bash's built-in echo command no longer† supports the -n option, because it's not a POSIX option. Instead, use the printf command to send control characters: printf '\\033]0;foo\\007' .

In general, it's better practice to use printf for consistent results instead of echo for anything more complicated than printing strings with ASCII graphic characters, because POSIX echo is fairly simple and any extended behaviors may vary between shells.

† I don't recall exactly which OS version it changed, but it's unsupported in 10.8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM