[Techtalk] shell scripting - modifying your prompt

Hans Tegnerud hans_tegnerud at chello.se
Tue Feb 11 23:42:16 EST 2003


> This is also where you can put in nifty colors if you want; eg, for your
> PIPESTATUS to show up in magenta you can put:
>
> export PS1="[ \[\e[35m\]\${PIPESTATUS[@]} \[\e[0m\]\u@\h \w ]$ "
>
> (here, the \[\e[35m\] says "start using magenta" and \[\e[0m\] says "start
> using whatever the normal color is")

I've also tried that, but I dropped it because bash didn't seem to treat the
escape characters the way I had expected. If you've managed to do this I'd
love to know how.

The problem I got after adding the escape characters is that bash seemed to
interpret them litterally, meaning a color switch (like: "\e[35m") was
treated as if the prompt got 6 characters longer. This lead to a CR
(carriage return) after bash decided the maximum screen width was exceeded
(prompt + command string) but without a LF (line feed)... the LF came
numerically where it should...

I'll try to give an example:
With a prompt like the one above I would (on a 80 character terminal) have
gotten a CR after [80 - (visible prompt) - command string = 69] characters
(11 escape characters). After another 11 characters overwriting my colorful
prompt I'd get a LF and continued on the next row.

Imagine my surprise when I started overwriting my then *very* nifty prompt
(with 6 color switches) after around 50 characters... :-/





More information about the Techtalk mailing list