让linux shell显示命令执行结果

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

在.bashrc文件中找到:

copy
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

在之后添加一段:

copy
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 @   王冰冰  阅读(121)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
历史上的今天:
2021-09-04 windows cmd 转义字符和双引号
2021-09-04 windows查看TCP连接
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起