Messed up bash prompt after printf
By Emma Martinez •
This annoying bug bothered me. When I type printf foo or echo -n foo, the bash prompt next becomes glitchy when I navigate through previous commands. Here is a snippet of my .bashrc
bash_get_exit_status(){ STATUS=$?; if [ $STATUS -eq 0 ]; then echo -en "\001\033[01;32m\002"; else echo -en "\001\033[01;31m\002"; fi; echo -n "[$STATUS]"; }
export PS1="\[\e]0;\u@\h: \w\a\]\$(bash_get_exit_status) \[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\\\$ "Output of bash --version:
bash --version
GNU bash, version 5.0.17(1)-release (aarch64-unknown-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.Behaviour of prompt:
echo -n foo #glitches prompt
echo -en "foo\nbar" #last line gets removed when scrolled up
echo -e "foo\nbar" #no problemI cannot find any results online. I gave up and asked it here.
41 Answer
bash_get_exit_status creates/includes a number of non-printing characters and codes in your prompt. These makes bash confused.
BACKGROUND:
You must have a \[ before, and \] after; thus enclosing the non-printing characters/codes/pieces.
More info:$ man bash type a single / and then PROMPTING directly after it and hit ENTER