Bash Prompt with Last Exit Code
So, I've been trying to customize by bash prompt so that it will look like
[feralin@localhost ~]$ _
with colors. I managed to get constant colors (the same colors every time I see the prompt) but I want the username ('feralin') to appear red, instead of green, if the last command had a nonzero exit status. I came up with:
\e[1;33m[$(if [[ $? == 0 ]]; then echo "\e[0;31m"; else echo "\e[0;32m"; fi)\u\e[m@\e[1;34m\h \e[0;35m\W\e[1;33m]$ \e[m
However, from my observations, the $(if ...; fi)
seems to be evaluated once, when the .bashrc
is run, and the result is substituted forever after. This makes the name always green, even if the last exit code is nonzero (as in, echo $?
). Is this what is happening? Or is it simply something else wrong with my prompt? Long question short, how do I get my prompt to use the last exit code?
As you are starting to border on a complex PS1, you might consider using PROMPT_COMMAND
.
With this, you set it to a function, and it will be ran after each command to generate the prompt.
You could try the following in your ~/.bashrc
PROMPT_COMMAND=__prompt_command # Func to gen PS1 after CMDs
__prompt_command() {
local EXIT="$?" # This needs to be first
PS1=""
local RCol='\[\e[0m\]'
local Red='\[\e[0;31m\]'
local Gre='\[\e[0;32m\]'
local BYel='\[\e[1;33m\]'
local BBlu='\[\e[1;34m\]'
local Pur='\[\e[0;35m\]'
if [ $EXIT != 0 ]; then
PS1+="${Red}\u${RCol}" # Add red if exit code non 0
else
PS1+="${Gre}\u${RCol}"
fi
PS1+="${RCol}@${BBlu}\h ${Pur}\W${BYel}$ ${RCol}"
}
This should do what it sounds line you want. Take a look a my bashrc's sub file if you want to see all the things I do with my __prompt_command
function.
If you don't want to use the prompt command there's two things you need to take into account:
- getting the value of $? before anything else, otherwise it'll be overriden
- escaping all the $'s in the PS1 (so it's not evaluated when you assign it)
Working example using a variable
PS1="\$(VALU="\$?" ; echo \$VALU ; date ; if [ \$VALU == 0 ]; then echo zero; else echo nonzero; fi) "
Working example without a variable
Here the if needs to be the first thing, before any command that would override the $?
.
PS1="\$(if [ \$? == 0 ]; then echo zero; else echo nonzero; fi) "
Notice how the \$()
is escaped so it's not executed right away but each time PS1 is used. Also all the uses of \$?
I wanted to keep default Debian colors, print the exact code, and only print it on failure:
# Show exit status on failure.
PROMPT_COMMAND=__prompt_command
__prompt_command() {
local curr_exit="$?"
local BRed='\[\e[0;91m\]'
local RCol='\[\e[0m\]'
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
if [ "$curr_exit" != 0 ]; then
PS1="[${BRed}$curr_exit${RCol}]$PS1"
fi
}
Improved @demure
I think this is important because there is not always exit status is 0 or 1.
if [ $EXIT != 0 ]; then
PS1+="${Red}${EXIT}:\u${RCol}" # Add red if exit code != 0
else
PS1+="${Gre}${EXIT}:\u${RCol}" # Also displays exit status
fi
ReferenceURL : https://stackoverflow.com/questions/16715103/bash-prompt-with-last-exit-code
'Nice programing' 카테고리의 다른 글
Python 클래스 정적 메서드 (0) | 2021.01.06 |
---|---|
IntellijIDEA가 Maven 종속성에 지정된 클래스를 인식하지 못함 (0) | 2021.01.06 |
C ++ 자동 및 자동 (0) | 2021.01.06 |
Amazon RDS MySQL 인스턴스에서 로컬 인스턴스로 데이터베이스를 내보내는 방법은 무엇입니까? (0) | 2021.01.06 |
update_item에서 if_not_exists와 list_append를 결합 할 수 있습니까? (0) | 2021.01.06 |