6

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.

4 Answers 4

10

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\] $
1

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.

1
  • 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.
    – Lalylulelo
    Feb 8, 2022 at 15:52
0

Solution 1:

You try something like this:

PS1="\[\033[36m\][\[\033[m\]\[\033[34m\]\[\e[1m \u@\h \e[0m\] \[\033[32m\]\W\[\033[m\]\[\033[36m\]]\[\033[m\] $"

For permanent change to bash prompt put it in .bashrc

Solution 2: Using tput

reset=$(tput sgr0)
bold=$(tput bold)
black=$(tput setaf 0)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
magenta=$(tput setaf 5)
cyan=$(tput setaf 6)
white=$(tput setaf 7)
user_color=$blue
PS1="\[$reset\]\[$cyan\][ \[$bold\]\[$user_color\]\u@\h\
\[$reset\]\[$blue\]\W\[$cyan\] ] \[$reset\]\[$reset\]\\$\[$reset\] "
4
  • sorry but that did nothing
    – Ashesh
    Sep 5, 2015 at 15:41
  • Please check this now it was wrong paste before.
    – snoop
    Sep 5, 2015 at 15:45
  • works but i lost all color
    – Ashesh
    Sep 5, 2015 at 16:39
  • This works perfectly fine in my 2 instance of Ubuntu 14.04.3 version. You might have some issue with character encoding.
    – snoop
    Sep 5, 2015 at 16:56
-3

If you are using ubuntu 18.04, run the following commands

cp /etc/skel/.bashrc ~/.profile

That's it. You'll have a bold user@host at the prompt

1
  • 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.
    – Félicien
    Jun 7, 2018 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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