让linux shell显示命令执行结果

vscode的shell可以用颜色来标记命令执行是否成功
image
如图,蓝色圆点表示成功,红色表示失败。但是默认的shell不可以。通过设置PS1可以实现这个功能。

在.bashrc文件中找到:

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

在之后添加一段:

old_color_prompt=$color_prompt
# BLACK='\[\e[0;30m\]'
RESET_COLOR='\[\033[00m\]'
GREEN='\[\033[0;32m\]'
RED='\[\033[0;31m\]'

PROMPT_COMMAND=smile_prompt

function smile_prompt
{
    last_state=$?

    if [ "$old_color_prompt" = yes ]; then
        if [ "$last_state" -eq "0" ]; then
            #smiley
            PS1=${GREEN}'• '${RESET_COLOR}'${CONDA_PROMPT_MODIFIER}${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
        else
            #frowney
            PS1=${RED}'• '${RESET_COLOR}'${CONDA_PROMPT_MODIFIER}${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
        fi
    else
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    fi

    case "$TERM" in
    xterm*|rxvt*)
        PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
        ;;
    *)
        ;;
    esac

}

这样就可以了,同时兼顾了conda添加的前缀。

效果如下:
image

posted @ 2023-09-04 16:48  王冰冰  阅读(105)  评论(0编辑  收藏  举报