Pete's Log: Somebody please explain this to me (learning bash the hard way)
Entry #2041, (Coding, Hacking, & CS stuff)(posted when I was 43 years old.)
This section of my .bashrc works perfectly fine in kitty, but leaves some extra garbage in my prompt (well, it repeats part of my prompt, but without expanding it) if I run bash in xterm or gnome-terminal:
This is what it's supposed to look like (as seen in kitty):
And here's the section in question:
PROMPT_COMMAND=__prompt_command __prompt_command() { local EXIT="$?" case "$TERM" in xterm*|rxvt*) PS1="\[\033]0;\]\u@\h: \w\[\007\]" ;; *) PS1="" ;; esac PS1+='[\[\033[1;34m\]\t\[\033[0m\]] \u@\h [\w]\n' if [ $EXIT != 0 ]; then PS1+="\[\033[31m\]prompt-fu\[\033[0m\]\$ " else PS1+="\[\033[32m\]prompt-fu\[\033[0m\]\$ " fi }
Eventually I realized that bash was printing my prompt out correctly, but the __prompt_command function was generating output when it ran:
I have no idea why, since the output appears to be generated when setting a variable. I was tempted to just leave things as they are, since it works in kitty, my current terminal emulator of choice, but it bugs me a little and I don't want my .bashrc to be dependent on running in a particular terminal emulator. I had found that if I take the color escape codes out of the prompt, then all works as intended, so I wondered if there was a chance I could pull apart the escape codes in the function and then concatenate them into the PS1 variable. E.g., split \033[1;34m into \033[1; and 34m. And to my surprise, delight, and dismay, it worked! So here's what that section now looks like:
PROMPT_COMMAND=__prompt_command __prompt_command() { local EXIT="$?" case "$TERM" in xterm*|rxvt*) PS1="\[\033]0;\]\u@\h: \w\[\007\]" ;; *) PS1="" ;; esac PS1+='[\[\033[1;' PS1+='34m\]\t\[\033[0' PS1+='m\]] \u@\h [\w]\n' if [ $EXIT != 0 ]; then PS1+="\[\033[31" else PS1+="\[\033[32" fi PS1+="m\]prompt-fu\[\033[0" PS1+="m\]\$ " }
All ANSI escape codes are now split across variables, and the result is that __prompt_command no longer generates any output in xterm or gnome-terminal. Recombining them into PS1 works as expected to make my prompt colorful. I don't particularly like this solution, but I don't feel like digging into this more right now.
The dotfiles saga continues...