Reputation: 782
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?
Upvotes: 2
Views: 2376
Reputation: 8349
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 important 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.Upvotes: 4
Reputation: 18703
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.
Upvotes: 2