I'm colorizing parts of my bash prompt using ANSI escape sequences. This works great, until the command I'm currently typing in is long enough that it has to wrap. Instead of the rest of the command displaying on the next line, it wraps back to column 1 of the current line, overwriting the beginning of the prompt.

I get that behavior with this prompt:

export PS1="[\u][\033[0;32;40mdemo \033[0;33;40m1.5.40.b\033[0;37;40m] \w> \033[0m"

but it works correctly with the same prompt, ANSI sequences remove:

export PS1="[\u][demo 1.5.40.b] \w> "

I'm connecting using the current version of Putty, with default Putty settings. The OS is Ubuntu 8.10.


It must be something in there because the following string from the bash documentation does not exhibit the same behavior:

PS1="\[\033[1;34m\][\$(date +%H%M)][\u@\h:\w]$\[\033[0m\] "

Haven't figured out what it is yet, but their works and yours doesn't...

EDIT: Try:

PS1="[\u][\[\033[1;32m\]\[\033[1;40m\]demo \[\033[1;33m\]\[\033[1;40m\]1.5.40.b\[\033[0;37m\]] \w> \[\033[0m\] "
  • 3
    That did it. The reason, it turns out, is that bash needs to be told "the following characters are non-printable" so it doesn't become confused over the length of the prompt. This is done by enclosing non-printable characters in \[ and \]. See tldp.org/HOWTO/Bash-Prompt-HOWTO/nonprintingchars.html – Eric J. Apr 12 '10 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.