How to make the terminal display user@machine in bold letters?
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 andCSI 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.
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
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\$ '
\[\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:
- See
man bash
and check thePROMPTING
section. - See ANSI escape code - Wikipedia.