Making bash prompt bold

16,115

Solution 1

Bold is set by 01 so you need to add 01; before each color specification:

\[\033[01;36m\][\[\033[m\]\[\033[01;34m\]\u@\h\[\033[m\] \[\033[01;32m\]\W\[\033[m\]\[\033[01;36m\]]\[\033[m\] $

Solution 2

I see there are other answers which are pretty much heuristic. However, if you have more specific needs (as you may do in future), I have a script which may be helpful to you.

# "Colorize" the plain text.
#
# Usage:
#
#   $ colorize "TEXT" COLOR ["STYLE"] [BACKGROUND]
#
# Notes:
#   - STYLE may be either a single value or a space-delimited string
#
# Examples:
#
#   $ colorize "Hey!" blue bold
#   $ colorize "Yo!" red italic white
#
colorize() {
  text="$1"

  if [ "$color_support" = true ]
  then
    color="$2"
    style=($3)
    background="$4"
    colors=(black red green yellow blue purple cyan white)
    styles=(regular bold italic underline reverse)
    sn=(0 1 3 4 7)

    for n in {0..7}
    do
      [[ $color == ${colors[n]} ]] && color="3$n"
      [[ $background == ${colors[n]} ]] && background="4$n"
      for s in ${!style[@]}
      do
        [[ ${style[s]} == ${styles[n]} ]] && style[s]="${sn[n]}"
      done
    done

    ! [ -z $style ] && style="${style[*]};" && style=${style// /;}
    ! [ -z $background ] && background=";$background"
    background+="m"

    text="\e[$style$color$background$text\e[0m"
  fi

  echo "$text"
}

It offers bold, italic, underline and reverse text styles, aswell as the supported colors in bash. You can also export the function in case you don't want to add it to .bash_profile directly.

Here is an example how you could use it for formatting the prompt (note the prompt requires a bit different syntax):

colorize_prompt() {
  colorize $@ &>/dev/null

  if [ "$color_support" = true ]
  then
    text="\[\e[$style$color$background\]$1\[\e[0m\]"
  fi

  echo $text
}

# Main prompt
PS1="$(colorize_prompt "火" purple bold) $(colorize_prompt "\w" blue bold) "

# Continuation prompt
PS2="$(colorize_prompt "|" cyan bold) "

The script is an exempt from my dotfiles.

Share:
16,115

Related videos on Youtube

Ashesh
Author by

Ashesh

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. ― Rick Cook

Updated on September 18, 2022

Comments

  • Ashesh
    Ashesh over 1 year

    My $PS1 variable is

    \[\033[36m\][\[\033[m\]\[\033[34m\]\u@\h\[\033[m\] \[\033[32m\]\W\[\033[m\]\[\033[36m\]]\[\033[m\] $
    

    I wish to maintain the same colors and text but make the prompt appear in bold. How do I accomplish this?

    I looked over the web and found this can be done using tput bold, but the prompt appeared broken to me, I must be doing it wrong.

  • Ashesh
    Ashesh over 8 years
    sorry but that did nothing
  • snoop
    snoop over 8 years
    Please check this now it was wrong paste before.
  • snoop
    snoop over 8 years
    This works perfectly fine in my 2 instance of Ubuntu 14.04.3 version. You might have some issue with character encoding.
  • Félicien
    Félicien almost 6 years
    This is not what the question asked for (he wanted to keep the colors he set). Moreover, if the user already uses a ~/.profile file, he will lose evrything he put inside.
  • Lalylulelo
    Lalylulelo about 2 years
    Cool function. It should be noted that you need to have somewhere the variable color_support sets as true, export color_support=true. It does not exist on my system. I had to add a -e in the last echo, echo -e $text.