Broken Mac OS X bash prompt while scrolling through history?

I have been trying some experiments with my bash prompt to make it stand out a little and also to give out more information, like which jailed root I’m in. The only issue that I’ve been facing so far is that the bash prompt breaks as you scroll through the history.

So that’s what my prompt looks like with the mentioned issue. As I scrolled through the history, an Apache bench command (ab -n 100 -c 10 ht..) got kind of stuck there and the previous command (cd /Volumes/) got overlapped with it. Some amount of debugging made it clear that it has got something to do with escaping of characters in my prompt.

Here’s what I used to have in my prompt:

export PS1='\n\[\]\e[0;34m\]\u \e[0;32m\]\h \e[0;31m\]$(set | grep ^JAIL_NAME= | cut -c11-) \e[0;35m\]\[\]\w\n\e[0;40m\] > \e[0m\] \[\]\e[0m\]'

The rewritten one which doesn’t have the issue is:

export PS1='\n\[\e[0;34m\]\u \[\e[0;32m\]\h $(set | grep ^JAIL_NAME= | cut -c11-) \[\e[0;35m\]\w \n\[\e[0;40m\] > \[\e[0m\] '

Apparently, I was not escaping the color coding properly. \[\e[ .. \] is the right way to put a color code. I’m guessing without escaping these characters were be counted as part of the prompt.

Tags: , , ,

  • http://www.zackhovatter.com Zack Hovatter

    Awesome. I read on another site an improper way of escaping the color coding. Now my terminal is back to working normally. Thanks for posting your solution.

Copyright (c) 2000-2011 Alex N J. All rights reserved.
All and any opinion expressed here are solely mine and do not represent the views of my employer or any other person or organization related to me. Entries (RSS) and Comments (RSS).