Wrapping lines bugs when trying to colour terminal PS1 (even when escaping non printing caracters)
The problem would be that (emphasis mine, from the bash manual, 6.9 Controlling the Prompt):
After the string is decoded, it is expanded via parameter expansion, command substitution, arithmetic expansion, and quote removal, subject to the value of the promptvars shell option (see Bash Builtins).
So, the \[
in your function output comes too late to tell Bash that control codes follow. You need to set PS1
with the output of the function included for this to work. I'd suggest using PROMPT_COMMAND
thus:
PROMPT_COMMAND='PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\](`batPer`)-\A:\[\033[01;34m\]\w\[\033[00m\]\$ "'
Note how the inner quotes are double quotes, so each time PS1
is set, the function output is added to it, and then Bash decodes PS1
to display the prompt.
Side note: with this, you don't need to echo -e
the output. Before, you needed that for colour since Bash wasn't interpreting it after expanding the PS1
string. Now, Bash will decode the control codes as well.