3

I use the default value of PS1 shipped with Ubuntu:

\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$

which is ok 90% of the time. However, when I cd to a symlink "directory" the name of the symlink, not the value of pwd -P is used.

How can I change the \w to get the real current directory? And still display with the format presented by \w or \W?

2 Answers 2

9

Use $(pwd -P) instead of \w . Like this:

PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]$(pwd -P)\[\033[00m\]$ '
6
  • 1
    Note that you'll have to do tilde substitution (a la \W) and trimming (a la PROMPT_DIRTRIM) yourself.
    – chepner
    May 22, 2013 at 14:32
  • @chepner Thanks for this note! I didn't had this in mind
    – hek2mgl
    May 22, 2013 at 14:33
  • I tried your idea. It didn't work for me. The prompt stays as it was in the directory where I set PS1. In other words, if I export PS1="\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]$(pwd -P)\[\033[00m\]$" in /home/wmiller/a/b/c then cd ~, I still see user@host:/home/wmiller/a/b/c$. Leaving out the double-quotes really does strange things, so that's not it.
    – Wes Miller
    May 22, 2013 at 15:17
  • use source ~/.bashrc to update the prompt.. (have tested it, with ubuntu, but note the comment of @chepner)
    – hek2mgl
    May 22, 2013 at 15:21
  • 2
    @WesMiller Use single quotes, so that the command substitution isn't evaluated until PS1 is actually expanded in your prompt. Otherwise, you hard-code your prompt to contain the directly where the command is first run, as you have observed.
    – chepner
    May 22, 2013 at 15:25
2

To expand on hek2mgl's answer, to make your prompt "automatic", use the PROMPT_COMMAND variable:

PROMPT_COMMAND='PS1="\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]$(pwd -P)\[\033[00m\]\$ "'

From the manual:

PROMPT_COMMAND

If set, the value is interpreted as a command to execute before the printing of each primary prompt ($PS1).

8
  • What does this accomplish? Single-quoting the prompt to prevent premature evaluation of any $( ) should suffice.
    – Daniel Beck
    May 22, 2013 at 17:59
  • The PROMPT_COMMAND is evaluated every time the prompt is about to be displayed, so the execution of pwd is delayed until then, and so it will always accurately reflect the current directory May 22, 2013 at 18:54
  • ... something that can be accomplished by single-quoting PS1 (see chepner's comment to the other answer). While this approach works, it seems unnecessarily intricate.
    – Daniel Beck
    May 22, 2013 at 18:57
  • @glennjackman I found a typo in your command string. You need a \ in front of the $ at the end. Thus: PROMPT_COMMAND='PS1="\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]$(pwd -P)\[\033[00m\]\$ "'. Without it, text entered on the command line wraps back to the beginning of the line at around position 80 with no linefeed, then clobbers everything in the prompt and the beginning of the command text, then continues past col 80 and wraps as usual to the next line. Weird. What is $ with no following text?
    – Wes Miller
    May 23, 2013 at 14:14
  • @WesMiller, "sometimes a cigar is just a cigar..." -- a $ with no variable name following is a literal $ -- test: echo "$ " May 23, 2013 at 14:45

You must log in to answer this question.

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