How to make the terminal display user@machine in bold letters?

77,018

Solution 1

You should be able to do this by setting the PS1 prompt variable in your ~/.bashrc file like this:

PS1='[\u@\h \w]\$ '

To make it colored (and possibly bold - this depends on whether your terminal emulator has enabled it) you need to add escape color codes:

PS1='\[\e[1;91m\][\u@\h \w]\$\[\e[0m\] '

Here, everything not being escaped between the 1;91m and 0m parts will be colored in the 1;91 color (bold red). Put these escape codes around different parts of the prompt to use different colors, but remember to reset the colors with 0m or else you will have colored terminal output as well. Remember to source the file afterwards to update the current shell: source ~/.bashrc

Solution 2

Find where your PS1 is set in your .bashrc and insert '\[\e[1m\]' at the beginning and \[\e[0m\] at the end.

  • \[ and \] are necessary so the shell knows the mess inside takes up 0 space on the screen, which prevents some screwed up behavior when doing line-editing. You don't need to worry too much about it.
  • \e[ is known as the CSI (control sequence introducer). You'll see it used in most of the codes listed on the referenced Wikipedia page. \e means the escape character.
  • If you look in the SGR table on the Wikipedia page, you'll see the 1 is the number for bright/bold text, and 0 is for reset. Thus CSI 1m turns on bold and CSI 0m resets the font so the rest of your text is normal.

Wikipedia has a full list of ANSI escape codes that you can use if your terminal emulator supports it.

Edit

For portability and readability, you should use tput instead of hard-coding escape codes. The only downside is the tput approach won't work with terminals that support ANSI codes but have broken or missing terminfo databases, but in that case the broken terminfo is a bigger problem as many of your console apps that rely on terminfo may not work properly.

Here's an example of what I do in my .bashrc:

# color names for readibility
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=$green
[ "$UID" -eq 0 ] && { user_color=$red; }
PS1="\[$reset\][\[$cyan\]\A\[$reset\]]\[$user_color\]\u@\h(\l)\
\[$white\]:\[$blue\]\W\[$reset\][\[$yellow\]\$?\[$reset\]]\[$white\]\
\\$\[$reset\] "

Here's what a genericized version of mine would look like. The 0 is the exit status of the last command.

PS1 screenshot

Solution 3

This is the default prompt that you get in cygwin bash shell:

PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ '

enter image description here

\[\e]0;\w\a\] = Set the Window title to your current working directory
\n            = new line
\[\e[32m\]    = Set text color to green
\u@\h         = display username@hostname
\[\e[33m\]    = Set text color to yellow
\w            = display working directory
\[\e[0m\]     = Reset text color to default
\n            = new line
\$            = display $ prompt

References:

Solution 4

I use this shell function to get text attributes in the terminal:

color () {
  if [ -z "$1" -a -z "$2" -a -z "$3" ]; then
    echo "\033[0m"
    return
  fi
  case $1 in
    black)   color_fg=30;;
    red)     color_fg=31;;
    green)   color_fg=32;;
    yellow)  color_fg=33;;
    blue)    color_fg=34;;
    magenta|purple) color_fg=35;;
    cyan)    color_fg=36;;
    white)   color_fg=37;;
    -)       color_fg='';;
    *)       color_fg=39;;
  esac
  case $2 in
    bold)      color_bd=1;;
    italics)   color_bd=3;;
    underline) color_bd=4;;
    inverse)   color_bd=7;;
    strike)    color_bd=9;;
    nobold)      color_bd=22;;
    noitalics)   color_bd=23;;
    nounderline) color_bd=24;;
    noinverse)   color_bd=27;;
    nostrike)    color_bd=29;;
    -)         color_bd='';;
    *)         color_bd=0
  esac
  case $3 in
    black)   color_bg=40;;
    red)     color_bg=41;;
    green)   color_bg=42;;
    yellow)  color_bg=43;;
    blue)    color_bg=44;;
    magenta|purple) color_bg=45;;
    cyan)    color_bg=46;;
    white)   color_bg=47;;
    -)       color_bg='';;
    *)       color_bg=49;;
  esac
  s='\033['
  if [ -n "$color_bd" ]; then
    s="${s}${color_bd}"
    if [ -n "$color_fg" -o -n "$color_bg" ]; then
      s="${s};"
    fi
  fi
  if [ -n "$color_fg" ]; then
    s="${s}${color_fg}"
    if [ -n "$color_bg" ]; then
      s="${s};"
    fi
  fi
  if [ -n "$color_bg" ]; then
    s="${s}${color_bg}"
  fi
  s="${s}m"
  echo "$s"
  unset s color_bd color_bg color_fg
}

Then to use it:

color_reset=`color`
color_grbd=`color green bold`
color_bubd=`color blue bold`
PS1="\[${color_grbd}\][\u@\h]\[${color_reset}\]:\[$color_budb}\]\w\[${color_reset}\]> "

This gives me [username@hostname]:cwd> where username@hostname are bold green and cwd is bold blue.

Solution 5

To make it look like a default Ubuntu prompt, add

export PS1='\[\e[1;32m\]\u@\h\e[0;39m\]:\e[1;34m\]\w\e[0;39m\]$\[\e[0m\] '

in your ~/.bashrc.

This will produce the well-known green user@host (bold), followed by a non-bold white colon, followed by your working directory in bold blue, followed by a $ sign, a space, and your command in non-bold white letters:

Ubuntu Bash prompt

Share:
77,018

Related videos on Youtube

neydroydrec
Author by

neydroydrec

Updated on September 18, 2022

Comments

  • neydroydrec
    neydroydrec almost 2 years

    I have seen in some screen-shots (can't remember where on the web) that the terminal can display the [username@machine /]$ in bold letters. I'm looking forward to getting this too because I always find myself scrolling through long outputs to find out with difficulty the first line after my command.

    How can I make the user name etc. bold or coloured?

    • Stephen Quan
      Stephen Quan over 12 years
      Can you clarify if you're using the bash shell or some other shell?
    • neydroydrec
      neydroydrec over 12 years
      Gnome Terminal bash.
  • neydroydrec
    neydroydrec over 12 years
    Nice and explanatory answer, +1. Still I found hesse's answer more to the point, so I chose it instead.
  • neydroydrec
    neydroydrec over 12 years
    Thanks for this. I suppose I also had to copy all of the above in ~/.bashrc?
  • jw013
    jw013 over 12 years
    Why not use 31 for red? 91 does not seem to be a standard code.
  • neydroydrec
    neydroydrec over 12 years
    Nice, I added a new line before the [user@host] which makes each command yet more readable (it should be set so by default on terminal imo).
  • Arcege
    Arcege over 12 years
    Yes, I set that in my ~/.bashrc file and use it to set my prompt. I even change the colors depending on the system I'm on.
  • Admin
    Admin over 12 years
    @jw013 The range 90-97 are equivalent to the 30-37 range if used as bold i.e. 1;31 and 1;91. Using normal font weight, the 90-97 range gives more brighter colors than the other.