How can I display the absolute path in bash prompt?

122,491

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\$ "
Share:
122,491

Related videos on Youtube

Moreno Ambrosin
Author by

Moreno Ambrosin

Updated on September 17, 2022

Comments

  • Moreno Ambrosin
    Moreno Ambrosin over 1 year

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

    • Admin
      Admin over 13 years
      /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 (/).
    • Admin
      Admin over 13 years
      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.
    • Admin
      Admin over 13 years
      @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?)
    • Admin
      Admin over 5 years
      \u@\H[\w]:~\$ makes user@host[~/path]:~$
  • Moreno Ambrosin
    Moreno Ambrosin over 13 years
    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.
  • Sirex
    Sirex over 13 years
    was the terminology in the url, not mine :)
  • Doug Harris
    Doug Harris over 13 years
    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.
  • cYrus
    cYrus over 13 years
    Right, missed that \, I usually work with single quotes if it's possible. Thanks.
  • Moreno Ambrosin
    Moreno Ambrosin over 13 years
    +1 Thanks! could you please explain the magic in your beautiful bash line (the 'tip' one)?
  • cYrus
    cYrus over 13 years
    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.
  • funroll
    funroll about 11 years
    minor point: per google's bash style guide, prefer $(pwd) to backticks because backticks require escaping when nesting and $() doesn't.
  • zamnuts
    zamnuts about 9 years
    The key here is \w gives the full path while \W gives only the directory. See "Bash Prompt Escape Sequences"