基于先前命令输出的颜色 PS1

Kar*_*rlo 3 command-line bash terminal bashrc

我目前的 PS1 如下。的$?输出是真正有用的(第二行)。

export PS1="\
${PSOn_Blue}${PSBWhite}\t\
${PSColor_Off} \$?\
${PSColor_Off}${PSBGreen} \u\
${PSColor_Off}${PSWhite}@\
${PSColor_Off}${hostcolor}\h\
${PSColor_Off}:\
${PSBGreen}\w\
${PSColor_Off}\$\
 "
Run Code Online (Sandbox Code Playgroud)

如果返回代码 ($?) 在非零输出上为红色,那就更好了。

我怎样才能做到这一点?

l0b*_*0b0 5

我用这个

BOLD_FORMAT="${BOLD_FORMAT-$(color_enabled && tput bold)}"
ERROR_FORMAT="${ERROR_FORMAT-$(color_enabled && tput setaf 1)}"
RESET_FORMAT="${RESET_FORMAT-$(color_enabled && tput sgr0)}"

PS1='$(exit_code=$?; [ $exit_code -eq 0 ] || printf %s $BOLD_FORMAT $ERROR_FORMAT $exit_code $RESET_FORMAT " ")'
Run Code Online (Sandbox Code Playgroud)

将其与$PS1. 如果您想显示退出代码,即使它为零,只需删除该[ $exit_code -eq 0 ] ||位。