Tonin pointed out a bug in my default prompt. Minimal example:
Set PS1:
PS1='$(exit_code=$?; [[ $exit_code -eq 0 ]] || printf %s $(tput setaf 1) $exit_code $(tput sgr0) " ")$ '
At this point, the prompt looks like this:
$
Now trigger the exit code output by running:
false
Now the prompt contains the exit code in red at the beginning of the line:
1 $
- Press Ctrl-r.
Type "false". Now the prompt contains only the search:
(reverse-i-search)`false': false
- Press Enter.
The resulting terminal history now contains the following:
1 $ch)`false': false
Expected output:
1 $ false
That is, it seems the history search output is mixed with the prompt and hiding the actual command which was run.
I tried working around this by using PROMPT_COMMAND
:
set_exit_code() {
exit_code=$?
[[ $exit_code -eq 0 ]] || printf %s $(tput setaf 1) $exit_code $(tput sgr0) " "
}
set_bash_prompt() {
PS1='$(set_exit_code)$ ' # Double quotes give the same result
}
PROMPT_COMMAND=set_bash_prompt
This doesn't seem to work - the line looks exactly the same as before after searching and running.
How can I fix this?