2

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

2
  • 2
    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, 2013 at 17:13
  • Did this answer your question?
    – ewwhite
    Jun 30, 2013 at 13:58

3 Answers 3

0

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$ \" )"
2

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

2

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]#
3
  • thank you but i ment more like /srv/myproject it would give prompt [root@server [myproject]#
    – Rubytastic
    Jun 25, 2013 at 17:09
  • The "w" option will show the FULL path... See my update.
    – ewwhite
    Jun 25, 2013 at 17:13
  • I pop up everywhere!
    – MDMarra
    Jun 26, 2013 at 1:50

You must log in to answer this question.

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