On Sun, 12 Mar 2017, Grisha Levit wrote:
For example:
$ PS1='\w $(echo \w) $ '
~ /home/levit $
This seems to work as documented, for the same reason this happens:
╶➤ echo ~
/home/rob
The expansion order is perhaps non-obvious: the prompt string backslash
escapes are replaced before the string is subject to any other expansions.
Obviously, this is easy enough to work around in most cases by
replacing \w with ${PWD/#$HOME/~}. I had a silly application for
calculating prompt length and this was throwing it off, but this
behavior has been there since at least 2.05b so not sure if it's worth
changing..
Doing anything that depends on calculating (any part of) the prompt length
at expansion time in PS1 is... complicated.
╶➤ echo ${#PS1}
1485
I have on occasion wondered how difficult it'd be to make ${#...}
applicable to any arbitrary expansion, but I also can't think of another
use case...
-Rob