Why is this bash prompt acting strangely/disappearing, and how do I fix it (OS X)?

◇◆丶佛笑我妖孽 提交于 2019-11-27 19:53:24
Chas. Owens

It sounds like this should solve your problem.

This seems to work for me*:

export PS1='--(\[\e[$((32-${?}))m\]\u\[\e[0m\])-(\[\e[$((32-${?}))m\]\d\[\e[0m\]|\[\e[$((32-${?}))m\]\T\[\e[0m\])--(\[\e[$((32-${?}))m\]\w\[\e[0m\] \$ '

* well, really export PS1='\u@\h:\w\$ ' works for me

To quote the linked post, the answer lies in adding \[ and \] around all of your color sequences in your PS1 declaration:

Before I had the following value for PS1:

'\e[0;34m\h:\w [!]\$\e[0m '

which gave me a nice blue prompt of the following form

hostname:working-directory [command-number]$

However, I had the same line-wrapping problem you did. The fix was to insert \[ and \] around the ANSI escapes so that the shell knows not to include them in the line wrapping calculation. This results in the following value for PS1:

'\[\e[0;34m\]\h:\w [!]\$\[\e[m\] '

lhunath

http://mywiki.wooledge.org/BashFAQ/053 -- I have a fancy prompt with colors, and now bash doesn't seem to know how wide my terminal is. Lines wrap around incorrectly.

By the way; for your reference; here's my PS1 which looks like this:


(source: lyndir.com)

\[$reset$bold$green\]\u@\h\[$blue\] \W \[$red\]${?/#0/\[$green\]}\$\[$reset\]

Notice how I put all the color codes in $parameters to make it neater, but more importantly, because you should be using tput to generate them. See:

http://mywiki.wooledge.org/BashFAQ/037 -- How can I print text in various colors?

I declare my color parameters in a utility script that gets sourced by my ~/.bashrc (and any scripts I write) which is called bashlib.

On a final note; put your PS1 definition in ~/.bashrc and don't export it. There's absolutely no reason why you should add your PS1 definition to the environment of any and all processes you spawn from your shell.

You just seem to be missing the start and end brackets around your escapes (before the first '\e' and after the last 'm'):

PS1='--(\[\e[$((32-${?}))m\u\e[0m)-(\e[$((32-${?}))m\d\e[0m|\e[$((32-${?}))m\T\e[0m)--(\e[$((32-${?}))m\w\e[0m\] \$ '

As mentioned, the PS1 var does not need to be exported: only your shell needs to see it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!