I recently tweaked a bit my PS1. The code as follows:PS1='$(if [[ $? != 0 ]]; then echo  \e[91m\e[0m; fi) \u $(assemble_prompt)$ 'The missing char is from nerd-fonts and doesn't matter in my question (as well as assemble_prompt function).The problem I encountered is a broken PS1 when I shrink terminal window size to the size of a prompt (approx.). Then it seem to ignore PS1 setting and sets PS1 to $ chars only (see screenshot). Note then when I start typing it simply overrides my custom prompt in this occasion.I narrowed the problem to an if clause that adds fail char if previous command was unsuccessful. If I remove that part everything works as expected.Is there a way to keep if clause part but fix the issue with PS1 reset when the window size is too small?P.S. I use rxvt-unicode as my terminal but the problem persists in all other terminals also (tested xterm, st).
Prompt customization problem with if clause
bash;prompt;escape characters;terminal emulator
You probably want to enclose the terminal control codes in \[...\] to tell Bash that they aren't actually printing characters. Otherwise they will confuse the shell when tries to count how wide the prompt is.So use \[\e[91m\]xxx\[\e[0m\].From the manual:\[  Begin a sequence of non-printing characters.     This could be used to embed a terminal control sequence into the prompt.\]  End a sequence of non-printing characters.