【发布时间】:2020-02-20 15:48:01
【问题描述】:
我希望在 Bash 和 Zsh 中都有相同的提示。我希望它:
- 如果最后一个命令失败,请按铃,并且
- 显示其错误代码。
在 Bash 中,我确实有:
BLK="\[$(tput setaf 0; tput bold)\]"
RED="\[$(tput setaf 1; tput bold)\]"
grn="\[$(tput setaf 2)\]"
GRN="\[$(tput setaf 2; tput bold)\]"
yel="\[$(tput setaf 3)\]"
reset_color="\[$(tput sgr0)\]"
PS1='\n\
`if [[ $? -gt 0 ]]; then printf "\[\033[01;31m\]$?"; tput bel; else printf "\[\033[01;32m\]0"; fi`\
\[\033]0;$PWD\007\] \
\[\033[0;32m\]\u@\h\
\[\033[01;30m\]:\
\[\033[;;33m\]\w\
\[\033[36m\]`__git_ps1`\
\[\033[0m\]\n$ '
在 Zsh 中,这是我的配置:
BLK=$(tput setaf 0; tput bold)
RED=$(tput setaf 1; tput bold)
grn=$(tput setaf 2)
GRN=$(tput setaf 2; tput bold)
yel=$(tput setaf 3)
reset_color=$(tput sgr0)
PROMPT="
%(?.$GRN.$RED)%?$reset_color $grn%n@%m$BLK:$reset_color$yel%~ $reset_color
%(!.#.$) "
这就是它在终端中的样子:
当最后一个命令出错时,两个提示都会响铃。 但是,在 Bash 中,它会打印 0 而不是正确的返回代码 失败了。
如何解决?
PS-欢迎任何改进上述代码的更好方法!
【问题讨论】:
标签: bash return-value command-prompt zsh prompt