Take the 2-minute tour ×

When i try to set my terminal window title:

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

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

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer

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() {
    # including the host name to disambiguate local vs.
    # remote connections. Percent-escape spaces.
    local SEARCH=' '
    local REPLACE='%20'

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.
share|improve this answer
still output -en –  gongqj Dec 17 '13 at 2:49
Perhaps you're using a shell other than bash? For example /bin/sh supports neither -e nor -n as options to its builtin echo. The system binary /bin/echo supports -n but not -e. And bash's builtin echo supports both -e and -n. I'm on OS X 10.9.1 which comes with bash 3.2 and it supports -en, I've also installed 4.2 and it still supports both. Though I think the default shell on OS X might be /bin/sh, try changing it with chsh -s /bin/bash and opening a new Terminal. –  onlynone Jan 23 at 22:43

Your Answer


