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

I currently have my bash PS1 set to something like this:

PS1="\[\`if [[ \$? = "0" ]]; then echo '\e[32m\h\e[0m'; else echo '\e[31m\h\e[0m' ; fi\`:\w\n\$ "

How can I make it show the absolute path instead of the relative one (e.g. /home/dave/dir instead of ~/dir)?

share|improve this question
/home/dave/dir and ~/dir are both absolute paths, the second uses an abbreviation for your home directory. A relative path is a path that is relative to your current directory (e.g. ../dir) rather than starting at root (/). – Doug Harris Oct 22 '10 at 13:38
p.s. Nice use of color to indicate exit status of previous command. Probably the first use of color in a prompt that I've liked. – Doug Harris Oct 22 '10 at 13:54
@Doug Harris: Thanks for the correction. I like this coloring, too. Don't remember where I first saw it (perhaps in some previous SU post?) – David B Oct 22 '10 at 17:15
up vote 22 down vote accepted

Just replace \w with the environment variable PWD:

PS1="\[\`if [[ \$? = "0" ]]; then echo '\e[32m\h\e[0m'; else echo '\e[31m\h\e[0m' ; fi\`:\$PWD\n\$ "

Anyway if you mind a little tip, I'd write something like:

PS1='\[`[ $? = 0 ] && X=2 || X=1; tput setaf $X`\]\h\[`tput sgr0`\]:$PWD\n\$ '
share|improve this answer
I thought of this, too, but the $PWD is evaluated at the time of the assignment to PS1, not on each call. However, if you escape the dollar sign, it will work -- \$PWD. With this, the dollar sign is a dollar sign at time of assignment and the variable evaluation happens on each prompt. +1 to you. – Doug Harris Oct 22 '10 at 15:47
Right, missed that \, I usually work with single quotes if it's possible. Thanks. – cYrus Oct 22 '10 at 16:06
+1 Thanks! could you please explain the magic in your beautiful bash line (the 'tip' one)? – David B Oct 22 '10 at 17:12
Sure. I used tput (see man tput for more info) to change the colors rather than write the control characters directly, for readability and portability (?). Also I removed double quotes and back slashes by using single quotes. Finally by using a variable I avoided some redundancy. – cYrus Oct 22 '10 at 17:30

Put in your home .bashrc

PS1='\u@\h:\w\$ '
share|improve this answer
The key here is \w gives the full path while \W gives only the directory. See "Bash Prompt Escape Sequences" – zamnuts May 4 '15 at 3:27

in bash's ps1; -W should be relative, and -w absolute, so in the above you should already have the absolute ?!

share|improve this answer
I think you're confusing absolute|relative with full|current_dir. W shows only the current directory name, w shows the full path, but still uses relative paths. – David B Oct 22 '10 at 9:41
was the terminology in the url, not mine :) – Sirex Oct 22 '10 at 9:57

Humm ~/dir is an absolute path but using a "shortcut". For instance, if you do cd /usr/local your prompt will most probably display the full path of /usr/local. So anyway, you have already a full path :-)

But probably your correct question is how to display the full path without any shortcuts like ~?

However, I don't know an answer for that one and looking at the man, it does seem to have one (at least documented).

share|improve this answer
Thanks for the correction. – David B Oct 22 '10 at 17:14

Run pwd instead of using \W.

Simple version:

export PS1="\`pwd\` $ "

Using this in your code:

export PS1="\[\`if [[ \$? = "0" ]]; then echo '\e[32m\h\e[0m'; else echo '\e[31m\h\e[0m' ; fi\`:\`pwd\`\n\$ "
share|improve this answer
minor point: per google's bash style guide, prefer $(pwd) to backticks because backticks require escaping when nesting and $() doesn't. – funroll Apr 11 '13 at 19:19

You must log in to answer this question.

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