Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I get the Terminal.app in OS X to display the current directory in its window or tab title?

I'm using the bash shell.

share|improve this question
I always wonder why not even people who answer the question remember to vote it up. –  Cawas Apr 1 '10 at 23:16

6 Answers 6

up vote 45 down vote accepted

Depends on your shell.

This article displays multiple methods.

I personally use zsh which has a convenient precmd() function which is run before each prompt.

    precmd () { print -Pn "\e]2;%n@%M | %~\a" } # title bar prompt

Although the other questions list bash methods, they alias cd. Bash provides an inherent method that chains off just the prompt.


bash supplies a variable PROMPT_COMMAND which contains a command to execute before the prompt. This example (inserted in ~/.bashrc) sets the title to "username@hostname: directory":

 PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'

where \033 is the character code for ESC, and \007 for BEL. Note that the quoting is important here: variables are expanded in "...", and not expanded in '...'. So PROMPT_COMMAND is set to an unexpanded value, but the variables inside "..." are expanded when PROMPT_COMMAND is used.

However, PWD produces the full directory path. If we want to use the '~' shorthand we need to embed the escape string in the prompt, which allows us to take advantage of the following prompt expansions provided by the shell:

\u          expands to $USERNAME
\h          expands to hostname up to first '.'
\w          expands to directory, replacing $HOME with '~'
\[...\]     embeds a sequence of non-printing characters

Thus, the following produces a prompt of "bash$ ", and an xterm title of "username@hostname: directory" ...

 case $TERM in
        PS1="\[\033]0;\u@\h: \w\007\]bash\$ "
        PS1="bash\$ "

Note the use of [...], which tells bash to ignore the non-printing control characters when calculating the width of the prompt. Otherwise line editing commands get confused while placing the cursor.

share|improve this answer
Should have mentioned I am using bash, I have updated the question. –  kareman Dec 7 '09 at 19:13
Added the bash method from the listed link. –  Darren Hall Dec 7 '09 at 19:17
Very good. To get only the path to the current directory in the title and the name of the current directory in the prompt I just used PS1="\[\033]0;\w\007\]\W \$ ". –  kareman Dec 7 '09 at 19:42
"If we want to use the '~' shorthand we need to embed the escape string in the prompt" You can use the following to replace the full home directory pathname with tilde: ${PWD/#${HOME}/\~} –  Chris Page Sep 19 '11 at 10:18
@MattTagg Sure, you just need to put it into your ~/.profile (or equivalent) file on the remote machine. –  CoreDumpError Jul 30 at 21:32

Assuming you are using the default MAC Terminal, you can use the following one in .profile since "set_prompt" by itself may send you to the root folder when you open a new tab:

set_prompt () {
    echo -ne "\033]0;$BASE_PATH\007"
set_my_tab () {

share|improve this answer

Copy & paste into file ~/.profile:
PROMPT_COMMAND='echo -ne "\033]0; ${PWD##*/}\007"'

This will set the title of the current terminal tab to the name of the folder you are in (NOT the whole path).
Developer/Applications/Utilities/Bluetooth/ becomes => Bluetooth

share|improve this answer
+1 for pragmatism –  88fingerskid Jul 10 '13 at 10:41
On mavericks the file to edit is ~/.bash_profile –  Tom May 15 '14 at 18:58

As of Mac OS X Lion 10.7, Terminal has an explicit escape sequence for setting the working directory, which Terminal displays using the standard window "proxy" icon. This enables you to Command-Click it to see the path, reveal in Finder, or drag it like any other folder. In addition, Terminal can use this to create another terminal at the same directory, and to restore the working directory when quitting/restarting Terminal (when Resume is enabled). It also enables restoring directories for Window Groups.

It's the same Operating System Command (OSC) escape sequence as for the window and tab titles, but with the first parameter set to 7. The value should be a "file:" URL, which enables percent-encoding special characters so it can handle all valid pathnames. You should also include the hostname so Terminal can determine whether it's a local directory; Terminal will avoid using it as the current working directory if it's from a different host.

On a related note, Terminal similarly supports setting the "represented file" using the OSC escape sequence with a parameter of 6. If set, the proxy icon will display this instead of the working directory. For example, I have emacs and less configured to reflect the currently displayed file/buffer in the proxy icon. This enables these tty-based programs to be more integrated with the surrounding OS.

The working directory behaviors are enabled by default for bash (the default shell on Mac OS X). See /etc/bashrc for the relevant code.

It's also probably worth mentioning that Lion Terminal now supports setting the tab title independently from the window title using the OSC escape sequence.

share|improve this answer
The update_terminal_cwd function will set the proxy icon to the current working directory. By default $PROMPT_COMMAND is update_terminal_cwd. –  jtbandes Sep 23 '14 at 18:35
@jtbandes That’s what I was referring to when I wrote “The working directory behaviors are enabled by default for bash…”. –  Chris Page Sep 24 '14 at 23:59
Yep. I just discovered how it works and thought it might be useful for future googlers! –  jtbandes Sep 25 '14 at 0:49

Bash solutions involving PS1 and the PROMPT_COMMAND both fail if the directory has Unicode characters in it (at least on Snow Leopard). The best solution I could come up with was to do the equivalent of

PS1="\[\033]0;\`pwd | tr -dC '[\000-\177]'\`\007\]\$ "

Unfortunately this doesn't work directly, so instead I defined a shell function to do the tr call:

termtitlefilter () { tr -dC '[\000-\177]'; }
PS1="\[\033]0;\`pwd | termtitlefilter\`\007\]\$ "

This works well for Latin accents, which will be in Unicode NFD (the accents will disappear but the underlying Latin character will remain).

Unfortunately, it will completely fail for things like Chinese.

share|improve this answer
As of Mac OS X Lion 10.7, Terminal has an explicit escape sequence for setting the working directory. It uses a URL, so you can use percent-encoding for special characters and support all valid pathnames. See my answer to this question for details. –  Chris Page Sep 19 '11 at 10:29

Enter this into your ~/.profile or equivalent file:

function settitle() { echo -n "]0;$@"; }
function cd() { command cd "$@"; settitle `pwd -P`; }

export PS1='\W \$ '

settitle `pwd`

The first line contains two special characters that can't be copied/pasted, but you can download the text from here: http://blog.nottoobadsoftware.com/files/setterminaltitle.sh.

share|improve this answer
"The first line contains two special characters that can't be copied/pasted" Instead of using echo use printf, which lets you express those characters (ESC and BEL/Control-G) with escape sequences: printf '\e]0;$@\a" –  Chris Page Sep 19 '11 at 10:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.