Shell sh prompt

I can't seem to get my intended prompt whenever my shell is sh(1):
Code:
PS1="^[[1;31m`pwd` #^[[0m "
gives
Code:
[1;31m/root #[0m
and behaves like this:
Code:
[1;31m/root #[0m cd /usr
[1;31m/root #[0m pwd
/usr
[1;31m/root #[0m _

I am truly baffled. Even csh is more predictable on something similar. Especially considering it's more probable for the terminal to screw up the codes...
 
I would like to just take that as an answer, but something doesn't seem right to me:
  • [1;31m is supposed to be caught and understood by the terminal/tty, not by the shell.
  • I can change the colors by doing echo "^[[1;31m" at the prompt, which prints a newline, followed by the prompt in the color specified.
  • The ^[ (alias \033) works in my .cshrc.
Ergo it is indeed possible – so why isn't sh escaping the ^[ characters? Is there any way to do so?

Btw I found the \w symbol which expands to the full path of the working directory – inelegant, but close enough, so `pwd` is no longer necessary.
 
Looks like it's taken literally. Try "\033[" instead of "^[[". Another way is to try to hit CTRL-V then ESC. That should produce the code for escape. And the sh(1) page also mentions:
Code:
 \e          The ESC character (ASCII 0x1b)

And
Code:
     Double Quotes
             Enclosing characters within double quotes preserves the literal
             meaning of all characters except dollar sign (`$'), backquote
             (``'), and backslash (`\').  The backslash inside double quotes
             is historically weird.  It remains literal unless it precedes the
             following characters, which it serves to quote:                
                                                                            
                   $     `     "     \     \n


So '\e[' (not "\e[") is also worth a try.
 
Back
Top