为什么这个 bash 提示行为奇怪/消失,我该如何修复它(OS X)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/706750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is this bash prompt acting strangely/disappearing, and how do I fix it (OS X)?
提问by David Smith
I admit that I use a somewhat long-winded bash prompt:
我承认我使用了一个有点冗长的 bash 提示:
--(username)-(Wed April 01|12:00:00)--(~ $
Recently, I got the bright idea to change it so that depending on the exit value from the previous command, if success, the interior elements of the ()'s would be green, and if failure, they would be red. I got it working for the most part (some odd exit statuses will change the color to something else, but I'm ok with it), but when typing a command which is more than one line, and causes the terminal to scroll, the prompt disappears! My prompt worked fine when there was no color, so I'm guessing it is related to my color escaping, and particularly my unclosed ['s, but I can't pin it down.
最近,我想到了一个好主意来改变它,根据上一个命令的退出值,如果成功,() 的内部元素将是绿色的,如果失败,它们将是红色的。我让它在大多数情况下都能正常工作(一些奇怪的退出状态会将颜色更改为其他颜色,但我可以接受),但是当键入超过一行的命令并导致终端滚动时,提示消失!当没有颜色时,我的提示工作正常,所以我猜这与我的颜色转义有关,尤其是我未关闭的 ['s,但我无法确定它。
#.profile
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 $ '
Thanks in advance!
提前致谢!
回答by Chas. Owens
It sounds like thisshould 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
*嗯,真的export PS1='\u@\h:\w\$ '对我有用
To quote the linked post, the answer lies in adding \[ and \]around all of your color sequences in your PS1 declaration:
引用链接的帖子,答案在于在PS1 声明中的所有颜色序列周围添加\[ 和 \]:
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\] '
在我对 PS1 有以下值之前:
'\e[0;34m\h:\w [!]\$\e[0m '
这给了我以下形式的漂亮蓝色提示
主机名:工作目录 [命令编号]$
但是,我遇到了与您相同的换行问题。修复方法是在 ANSI 转义符周围插入 \[ 和 \],以便 shell 知道不要将它们包含在换行计算中。这导致 PS1 的以下值:
'\[\e[0;34m\]\h:\w [!]\$\[\e[m\] '
回答by 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.
http://mywiki.wooledge.org/BashFAQ/053-- 我有一个带有颜色的花哨提示,现在 bash 似乎不知道我的终端有多宽。线条错误地环绕。
By the way; for your reference; here's my PS1which looks like this:

(source: lyndir.com)
顺便一提; 供你参考; 这是我的PS1,看起来像这样:(
来源:lyndir.com)
\[$reset$bold$green\]\u@\h\[$blue\] \W \[$red\]${?/#0/\[$green\]}$\[$reset\]
Notice how I put all the color codes in $parametersto make it neater, but more importantly, because you should be using tputto generate them. See:
请注意我如何将所有颜色代码放入$parameters以使其更整洁,但更重要的是,因为您应该使用tput它们来生成它们。看:
http://mywiki.wooledge.org/BashFAQ/037-- How can I print text in various colors?
http://mywiki.wooledge.org/BashFAQ/037-- 如何打印各种颜色的文本?
I declare my color parameters in a utility script that gets sourced by my ~/.bashrc(and any scripts I write) which is called bashlib.
我宣布在得到由采购实用脚本我的颜色参数我~/.bashrc(我写的任何脚本)这就是所谓bashlib。
On a final note; put your PS1definition in ~/.bashrcand don't exportit. There's absolutely no reason why you should add your PS1definition to the environment of any and all processes you spawn from your shell.
最后一点;把你的PS1定义放进去~/.bashrc,不要export。绝对没有理由将您的PS1定义添加到您从 shell 生成的任何和所有进程的环境中。
回答by glenn Hymanman
You just seem to be missing the start and end brackets around your escapes (before the first '\e' and after the last 'm'):
您似乎缺少转义符周围的开始和结束括号(在第一个 '\e' 之前和最后一个 '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.
如前所述,PS1 var 不需要导出:只有您的 shell 需要查看它。

