Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The way my bash prompt is currently configured, it shows the whole path to the current directory. This is annoying when I'm deep inside a directory tree, as the prompt becomes so long that every command wraps into the next line. How do I make it show only the last part of the path?

This is what I have in my .bashrc:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

# If this is an xterm set the title to user@host:dir
case "$TERM" in
    PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'
share|improve this question
up vote 43 down vote accepted

Change the \w (lowercase) to \W (uppercase):

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '
           this one waaaaaay over here ------------------------------------------------+

Have a look at the Bash Prompt HOWTO for lots of fun details. example:

user@host:/usr/local/bin$ echo $PS1

user@host:/usr/local/bin$ export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\W\[\033[00m\]\$ '


The PROMPT_COMMAND variable, if set, is a command that gets run before displaying the prompt specified in PS1. In your case, PROMPT_COMMAND runs an echo statement with certain ANSI escape sequences that manipulate the titlebar of an Xterm.

If you suspect your PROMPT_COMMAND is overriding your PS1 prompt, you can unset it and test things out:


Finally, be sure that you're changing the PS1 definition that actually gets used. Common locations are /etc/bash.bashrc, /etc/profile, ~/.bashrc, ~/.bash_profile, ~/.profile. The system files are generally (but not always) run before the user files.

share|improve this answer
Hm... I'm afraid it's already \w, but it seems like that case statement overrides it when i'm on an xterm, and the problem seems to be with the PWD in the PROMPT_COMMAND line. Do you know what I should put there? – obvio171 Oct 25 '09 at 22:48
\w (lower case) sets it to full path, \W (uppercase) trims to the final bit. the PROMPT_COMMAND is setting the window title in an xterm. check your TERM variable; if it doesn't start with "xterm" or "rxvt" then PROMPT_COMMAND isn't even getting run. – quack quixote Oct 25 '09 at 22:59
Oh, yeah, duh. Sorry, I mixed upper and lowercase. That worked. Thanks! :) – obvio171 Oct 25 '09 at 23:39

Simple bash replace command is


For showing the last directory you can just do ${PWD/*\//}, i.e. find any thing before and including the last '/' and replace it with nothing.

On my ubuntu machine I use:

export PS1='$(whoami):${PWD/*\//}#'. 
share|improve this answer
Only six years late! – Burgi Jun 7 at 8:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .