Bash shell prompt: where is $RET?

Posted by Evgeni Sergeev on Super User See other posts from Super User or by Evgeni Sergeev
Published on 2012-06-23T04:35:08Z Indexed on 2012/06/23 9:20 UTC
Read the original article Hit count: 232

Filed under:

I was reading this https://wiki.archlinux.org/index.php/Color_Bash_Prompt and ended up with the following:

# Stores the status of each command in $RET
PROMPT_COMMAND='RET=$?;'

# A colour.
RED_SHELL='\e[0;36m'

# Prints "Status 1" if RET is 1, for example.
RET_VISUALISE='$(if [[ $RET != 0 ]]; then echo -ne "Status \[$RED_SHELL\]$RET\n" && RET=0; fi;)'

# What to print for each prompt.
PS1="$RET_VISUALISE\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \t \[\e[33m\]\w\[\e[0m\]\n\$ "

This does almost what I want, except when I press Enter,Enter,Enter multiple times after a command that returned status != 0. In this case it prints "Status 1" every time I press Enter.

This is what the && RET=0; part was supposed to get rid of.

Also, I don't understand why env | grep RET only shows the PS1 contents. What is the scope of $RET ?

© Super User or respective owner

Related posts about bash