如何根据上一个命令的退出代码更改bash提示颜色? [英] How to change bash prompt color based on exit code of last command?
问题描述
如果先前的退出代码为0,我想显示绿色的笑脸,如果失败则显示红色的笑脸.
I want to display a green smile face if the previous exit code is 0 and red smile face if not successful.
基本上,我想执行此提示,但要包含其他内容.
Basically I want to do this prompt but with other stuff included.
PS1='\u@\h:\w `if [ $? = 0 ]; then echo \[\e[32m\]:\)\[\e[37m\]; else echo \[\e[31m\]:\(\[\e[37m\]; fi` $ '
我想将条件逻辑抽象为一个函数,但是当我尝试将这两个逻辑结合在一起时,会显示转义字符而不是颜色.
I want to abstract the condition logic to a function but when I try to combine these two the escape characters show instead of the colors.
smiley()
{
if [ $? == 0 ]; then
echo ':)'
else
echo ':('
fi
}
RED="\033[1;5;91m"
GREEN="\033[1;5;92m"
NONE="\033[m"
NORMAL="\[\033[0m\]"
YELLOW="\[\033[1;4;93m\]"
MAGENTA="\[\033[35m\]"
WHITE="\[\033[1;37m\]"
BLINK="\[\033[5m\]"
#INVERT="\[\e[7m\]"
#OFF="\[\033[m\]"
PS1="${YELLOW}\u${MAGENTA}@${YELLOW}\h${NORMAL}:${WHITE}\w $(smiley)\n"
我什至尝试了一条线,但是也没有用.
I even tried one line but it didn't work either.
PS1='\[\033[1;4;93m\]\u\[\033[35m\]@\[\033[1;4;93m\]\h\[\033[0m\]\[\033[1;37m\] \W if [ $? = 0 ]; then echo \[\e[32m\]:\)\[\e[37m\]; else echo \[\e[31m\]:\(\[\ e[37m\]; fi\n'
如果没有PROMPT_COMMAND,有什么方法可以做到这一点?
If there any way to do this without PROMPT_COMMAND?
推荐答案
我认为您的报价不正确.我对此进行了一些摆弄,终于使它起作用了:
I assume your quoting is not correct. I fiddled a little bit around with this and finally got it working:
$ bash --version
GNU bash, version 4.4.12(3)-release (i686-pc-cygwin)
$ smiley()
> {
> if [ "$?" == "0" ]; then
> echo -e '\e[0;32m:) '
> else
> echo -e '\e[0;31m:( '
> fi
> }
$ PS1="$PS1"'`smiley`'
$ :) rm non-existing
rm: cannot remove 'non-existing': No such file or directory
$ :( echo "Everything fine"
Everything fine
$ :)
我是在Windows(64位)上执行此操作的,但我猜它也应该在Linux(或其他类似Unix的版本)上也可以工作.
I did this on Windows (64 bit) but I guess it should work on Linux (or any other Unix-like) as well.
注意:
-
我编写了一个函数
smiley()
(您的简化版本),并通过从命令行调用它进行了检查.效果很好.
I wrote a function
smiley()
(a simplified version of your) and checked it by calling it from command line. It worked fine.
我将它添加到了PS1
中,并且在任何情况下都回显了:)
.我意识到bash替换已在PS1
的分配中完成.
I added it to PS1
and it echoed :)
in any case. I realized that the bash replacement was already done in assignment of PS1
.
因此,我用一对额外的单引号保护了smiley
的调用安全,以将调用推迟到提示输出之前.现在,它的工作与预期的一样.
Thus, I safed the invocation of smiley
by an extra pair of single quotes to defer the invocation until the output of prompt. Now, it works like expected.
因为发问者需要彩色版本,所以我进行了更新.我在此链接中找到了实际的解决方案: SO:如何在Linux中更改echo的输出颜色.很容易找到必要的终端转义序列.诀窍是使用echo -e
启用echo
中的反斜杠转义.
Because the questioner required a colored version I made an update. I found the actual solution in this link: SO: How to change the output color of echo in Linux. It's easy to find the necessary terminal escape sequences. The trick is to use echo -e
to enable the backslash escaping in echo
.
下面的快照显示了它的外观(带有颜色):
The snapshot below shows how does it look (with colors):
这篇关于如何根据上一个命令的退出代码更改bash提示颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!