I'm trying to customize the bash prompt and here is my PROMPT_COMMAND

prompt_command() {
  local time="${BLUE}\t${RESET}"
  local user="${GREEN}\u${RESET}"
  local hostname="${GREEN}\H${RESET}"
  local current_dir="${YELLOW}\w${RESET}"

  PS1="⌂ ${time} ${user}@${hostname} ${current_dir} \n ➤ "

And when I try to type something (let it be whoami) it looks like bash determines position of the cursor incorrectly. I almost don't see the last (rightmost) symbol (i in this example) i.e the cursor overlaps it.

I said "wide" because when I replace with one of $, ш, symbols everything is OK. I tried another arrows from here with the same result.

Could anyone explain what's happening (or just give me "bash-compatible" arrow) please?

  • 3
    Bash is completely innocent here; it does not even have the slightest idea of character width. That's your terminal emulator which (out of the goodness of its little heart) allows the use of non-monospace fallback fonts. Use your favorite character map application and choose a character which is actually present in the font selected for the terminal emulator. – AlexP Jul 27 '18 at 15:38
  • 1
    As mentioned in the other comment, if it works with another multi byte character, it is a bug in the terminal and not in bash. – schily Jul 27 '18 at 15:51
  • @AlexP, thank you, but I figured out that the problem has nothing to do with fonts (see my answer) – Mestake Jul 28 '18 at 11:24

It appears the problem was a bit more complicated. The real problem was that I defined my colors like that:

[ -z "$TPUT" ] && TPUT=tput
RESET="$(     $TPUT sgr0)"    # Reset all attributes
BRIGHT="$(    $TPUT bold)"    # Set “bright” attribute
BLACK="$(     $TPUT setaf 0)" # foreground to color #0 - black
RED="$(       $TPUT setaf 1)" # foreground to color #1 - red
GREEN="$(     $TPUT setaf 2)" # foreground to color #2 - green
YELLOW="$(    $TPUT setaf 3)" # foreground to color #3 - yellow
BLUE="$(      $TPUT setaf 4)" # foreground to color #4 - blue
MAGENTA="$(   $TPUT setaf 5)" # foreground to color #5 - magenta
CYAN="$(      $TPUT setaf 6)" # foreground to color #6 - cyan
WHITE="$(     $TPUT setaf 7)" # foreground to color #7 - white
FGDEFAULT="$( $TPUT setaf 9)" # default foreground color

But according to this post all the color sequences inside the PS1 line must be enclosed within \[ \] (I woudn't even think it has something to do with my case but I discovered the same history-overlapping effect as mentioned in that question).

So I changed my colors:

[ -z "$TPUT" ] && TPUT=tput
RESET_ESC="\[$(     $TPUT sgr0)\]"
BRIGHT_ESC="\[$(    $TPUT bold)\]"
BLACK_ESC="\[$(     $TPUT setaf 0)\]"
RED_ESC="\[$(       $TPUT setaf 1)\]"
GREEN_ESC="\[$(     $TPUT setaf 2)\]"
YELLOW_ESC="\[$(    $TPUT setaf 3)\]"
BLUE_ESC="\[$(      $TPUT setaf 4)\]"
MAGENTA_ESC="\[$(   $TPUT setaf 5)\]"
CYAN_ESC="\[$(      $TPUT setaf 6)\]"
WHITE_ESC="\[$(     $TPUT setaf 7)\]"
FGDEFAULT_ESC="\[$( $TPUT setaf 9)\]"

And the problem (both problems) are gone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.