更正 BASH 提示中的 return 值

Correct return value in BASH prompt

这是在 Ubuntu 19.10 机器上设置提示的 .bashrc 文件的一部分:

# Terminal tab name in gnome-terminal or Guake
PROMPT_COMMAND='echo -ne "3]0;$(basename ${PWD})[=12=]7"'

GIT_PS1_SHOWCOLORHINTS=yes
RET=$?
source /usr/lib/git-core/git-sh-prompt

if [ "$color_prompt" = yes ]; then
    color_reset=$(tput sgr0)
    color_bold=$(tput bold)
    color_white=$(tput setaf 7)
    color_jobs=$(tput setaf 7)
    color_user=$(tput setaf 3)
    color_dir=$(tput setaf 4)
    color_load=$(tput setaf 5)
    color_succeed=$(tput setaf 2)
    color_fail=$(tput setaf 1)
    sep=$(tput setaf 7)\)
    PS1="${color_user}\u${sep}\[\D{%T}\]${sep}${color_reset}$(__git_ps1)${color_dir}\W${color_reset}${sep}\`if [[ ${RET} == 0 ]]; then echo \"${color_succeed}0\"; else echo \"${color_fail}${RET}\"; fi\`${color_white}${sep}${color_reset}$ "
else
    PS1="\u${sep}\[\D{%T}\]${sep}$(__git_ps1)\W${sep}\`if [[ ${RET} == 0 ]]; then echo \"0\"; else echo \"${RET}\"; fi\`${sep}$ "
fi

这是正在使用的命令行示例:

snim2)10:39:53)dirname)0)$ cd scratch
snim2)10:39:54)scratch)0)$ git init
Initialised empty Git repository in .../scratch/.git/
snim2)10:39:56) (master)scratch)0)$ asdasda
asdasda: command not found
snim2)10:40:05) (master)scratch)1)$ $?
127: command not found
snim2)10:40:10) (master)scratch)1)$ 

如您所见,当命令的 return 代码不为零时,它在提示中显示为 1),而不是显示正确的 return 代码 - - 在这种情况下 127)。如何解决?

我没看到你在哪里更新了 RET 的值。对动态部分使用 PROMPT_COMMAND

$ PS1='[$?=$RET]$ '; PROMPT_COMMAND='RET=$?; if [ "$RET" -eq 0 ]; then RET="good $RET"; else RET="bad $RET"; fi'
[$?=good 0]$ echo okay
okay
[$?=good 0]$ lkjjlk
-bash: lkjjlk: command not found
[$?=bad 127]$ echo $?
127
[$?=good 0]$ ls lkjjl
ls: cannot access 'lkjjl': No such file or directory
[$?=bad 2]$ ls lkjjl; echo $?
ls: cannot access 'lkjjl': No such file or directory
2
[$?=good 0]$

你这样做:

..$(__git_ps1)...\`if [[ $? == 0 ]];

$? 将成为 __git_ps1 的 return 状态,而不是命令行上最后执行的命令。

立即尝试在第一个命令替换块中保存退出 return 值:

if [ "$color_prompt" = yes ]; then
    color_reset=$(tput sgr0)
    color_bold=$(tput bold)
    color_white=$(tput setaf 7)
    color_jobs=$(tput setaf 7)
    color_user=$(tput setaf 3)
    color_dir=$(tput setaf 4)
    color_load=$(tput setaf 5)
    color_succeed=$(tput setaf 2)
    color_fail=$(tput setaf 1)
    sep=$(tput setaf 7)\)
else
    color_reset=
    color_bold=
    color_white=
    color_jobs=
    color_user=
    color_dir=
    color_load=
    color_succeed=
    color_fail=
    sep=\)  # remove the braces...
fi

# note the quotes - "" expand at setting time, '' expand at runtime
PS1=
PS1+="${color_user}\u${sep}\[\D{%T}\]${sep}${color_reset}"
PS1+='$('
   PS1+='ret=$?; '  # first thing we do - save the exit return value
   PS1+='__git_ps1; '
   PS1+='printf "%s" "'
      PS1+="${color_dir}\W${color_reset}${sep}"
      PS1+='"; '
   PS1+='if ((ret == 0)); then '
       PS1+='printf "%s" "'
          PS1+="${color_succeed}"  # expand variable on assignment side
          PS1+='0"; '  # this looks strange. Just print first the color, then $ret...
   PS1+='else '
      PS1+='printf "%s" "'
           PS1+="$color_fail"
           PS1+='$ret"; '
   PS1+='fi '
PS1+=')'
PS1+="${color_white}${sep}${color_reset}"
PS1+='$ '  # note - this is *not* "$ "