Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

Is it possible to have the name of the current working directory in the prompt in shell ?

Im working on OSX and CentOS in multiple projects under /srv/... and a mistake for wrong directory is quickly made.

I would like to have

[current_path] in my prompt somehow , perhaps coloured Is this possible?

For example like /srv/myproject it would give prompt [root@server [myproject]# This should stay the same even in /srv/myproject/app/etc

share|improve this question
If you're in /srv/myproject/app/etc/ then how should it determine that you want to see [myproject] instead of [app] or [etc]? – DerfK Jun 25 '13 at 17:13
Did this answer your question? – ewwhite Jun 30 '13 at 13:58
up vote 0 down vote accepted

This should be close to what you're looking for:

PS1="\$([[ \$PWD == /srv/*/* ]] && p=\${PWD#/*/} && p=[\${p%%/*}]; echo -n \"\$p$ \" )"

For testing, use this to show the full directory along with the truncated one for comparison:

PS1="\$([[ \$PWD == /srv/*/* ]] && p=\${PWD#/*/} && p=[\${p%%/*}]; echo -n \"[\$PWD] \$p$ \" )"

The escapes cause the variable expansion to be deferred until the prompt is issued. The parameter expansions strip off the first level directory and all directory levels below the second level.

You will probably need:

shopt -s promptvars

but I think it's the default.

The prompt only does its job when you're in a directory under /srv

This is one way to add some color. It makes the project name cyan.

PS1="\$([[ \$PWD == /srv/*/* ]] && p=\${PWD#/*/} && p='[\[\033[1;36m\]'\${p%%/*}'\[\033[0m\]]'; echo -n \"\$p$ \" )"
share|improve this answer

The character sequence for the full current working directory in $PS1 is \w. See "Tip: Prompt magic" for more details.

share|improve this answer

I use the following string in /etc/bashrc on my Linux systems. Usually it's a matter of replacing an upper-case "W" with lower-case "w" on most default Bash installations:

PS1="[\u@\h \w]\\$ "

This yields:

[root@MDMarra /ppro/edi/0010/GOODBI/receive/archive]#  

instead of:

[root@MDMarra archive]#
share|improve this answer
thank you but i ment more like /srv/myproject it would give prompt [root@server [myproject]# – Rubytastic Jun 25 '13 at 17:09
The "w" option will show the FULL path... See my update. – ewwhite Jun 25 '13 at 17:13
I pop up everywhere! – MDMarra Jun 26 '13 at 1:50

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.