How can I display the absolute path in bash prompt?

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)?


Solution 1:

Just replace \w with \$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\$ '

Solution 2:

Put in your home .bashrc

PS1='\u@\h:\w\$ '

Solution 3:

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