Custom prompt doesn’t work on Mac Terminal

bashprompt

I like to use a custom prompt (current path in blue) on my unix machine:

export PS1='\[\e[0;34m\]\w \$\[\e[m\] '

But when I try to use it on Mac's terminal it doesn't work: it fails to detect the end of the prompt and overwrites the prompt when I type commands. This also happens when I'm inputting a long command where it wraps over the same line instead of starting a new line.

I don't understand why this is the case since I use bash on both machines. Any suggestions on how to remedy this?

Best Answer

I'm not sure why you are getting different results, but the correct value should be

export PS1='\[\e[0;34m\]\w \$\[\e[m\] '

Non-printing characters should be enclosed in \[ \], so that bash can compute the correct length of the prompt on the screen (for cursor-positioning purposes).

Related Question