Reputation: 49
I am setting up my bash prompt in .bashrc using the following (simplified) function:
set_prompts() {
PS1="\u@\h in \w "
PS1+="\$(get_git_repo_details)"
PS1+="\n"
PS1+="\$(exit_status_prompt)"
}
Now the exit_status_prompt
prints a different coloured prompt character, depending on whether the value of $?
is 0 or not.
What I noticed though, is that with the code as above, the colour of the prompt character never updates. However, if I append the output of exit_status_prompt
to $PS1
before I append the output of get_git_repo_details
, or don't append the output of get_git_repo_details
at all, then it does update.
Does anyone know what is causing this? Thanks.
Edit:
exit_status_prompt()
{
if [ $? -ne 0 ]
then
highlight 1 "❯ "
else
highlight 2 "❯ "
fi
}
The highlight
function then just uses tput
to prepend the string in the second parameter with the colour specified in the first parameter.
Upvotes: 3
Views: 371
Reputation: 531918
You need to call exit_status_prompt
before doing anything else in set_prompts
, or $?
is going to be reset. Presumably, exit_status_prompt
uses the exit status of the most recently executed command or assignment.
set_prompts() {
esp=$(exit_status_prompt)
PS1="\u@\h in \w "
PS1+="$(get_git_repo_details)"
PS1+="\n"
PS1+="$esp"
}
I've unescaped the command substitutions, because I assume that you are (and should be) running set_prompts
as the first command in PROMPT_COMMAND
.
Upvotes: 2