ryanwhocodes Dec 20, 2018 · 7 min read

How to Christmassify your terminal and Bash shell

Customise your shell prompt using Bash variables, functions, escaped characters, emojis, colors and font styles

How to add emojis, color, and font styles to your Bash prompt.

This tutorial will show you how to customise your shell prompt using Bash variables and escaped characters. You will learn how to add emojis, colors, font styles, as well as run commands that execute every time the prompt is drawn, such as to display your git branch.

While this tutorial has a seasonal theme, it can serve as a basis for adding ANY types of emojis, color schemes, and functions you like to your shell.


PS1 and backslash escaped special characters

To change the prompt, I will show you how to set the PS1 environment variable, which is usually set in your ~/.bashrc.

PS1 means prompt string 1. There are four in total — see the Bash main page for more explanation.

Bash has various backslash-escaped special characters that you can use to build in the information and style you want. For example, to display the shell name (\s), your current directory (\W), and a prompt character (\$) you would set the following:

PS1="\s \W \$ "

The following shows all the options as listed in the Bash manual reference:

When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters that are decoded as follows:

 \a an ASCII bell character (07)
 \d the date in "Weekday Month Date" format (e.g., "Tue May 26")
 the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
 \e an ASCII escape character (033)
 \h the hostname up to the first `.'
 \H the hostname
 \j the number of jobs currently managed by the shell
 \l the basename of the shell's terminal device name
 \n newline
 \r carriage return
 \s the name of the shell, the basename of $0 (the portion following the final slash)
 \t the current time in 24-hour HH:MM:SS format
 \T the current time in 12-hour HH:MM:SS format
 \@ the current time in 12-hour am/pm format
 \A the current time in 24-hour HH:MM format
 \u the username of the current user
 \v the version of bash (e.g., 2.00)
 \V the release of bash, version + patch level (e.g., 2.00.0)
 \w the current working directory, with $HOME abbreviated with a tilde (uses the value of the PROMPT\_DIRTRIM variable)
 \W the basename of the current working directory, with $HOME abbreviated with a tilde
 \! the history number of this command
 \# the command number of this command
 \$ if the effective UID is 0, a #, otherwise a $
 \nnn the character corresponding to the octal number nnn
 \\ a backslash
 \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
 \] end a sequence of non-printing characters


You can use emojis in your prompt by copying and pasting them into where you set your PS1 variable. There are many places you can find lists of emojis, such as emojipedia.org.

PS1="🎄\s \W \$ "

Color and font style using tput

Key information about a terminal is kept in the terminfo database within your terminal program. We are going to use the command tput to do the font and color styling, which looks up information in the terminfo database to be interpreted by the shell and terminal.

If you are using iTerm2 on a Mac, you can set the ANSI colors used by your terminal by going to: iTerm2 –> Preferences –> Profiles –> Colors

I use the Dracula color scheme. If you want to view a large selection, which you can then download and Import in the iTerm2 Color Presets, then check out iterm2colorschemes.com.

The way that the shell knows what format to print a line in is through a combination of printable characters, which are the text content, and non-printable characters, which contain the formatting information.

  • [ starts off a series of non-printable characters
  • ] tells the shell the non-printable character sequence has ended

IMPORTANT! You must use both of these, otherwise the shell will not know where to position the cursor properly as it will think the non-printable characters are characters within a line.

Within a pair of opening and closing escaped square brackets you can add in the formatting information, such as the information provided with tput. To evaluate tput commands you put them inside command substitution brackets $().

For more on the syntax of command substitution in Bash, see StackOverflow’s discussion on Backticks vs braces in Bash.

To set a font color you use tput setaf with the number of the color you want.

Value Color
0 Black
1 Red
2 Green
3 Yellow
4 Blue
5 Magenta
6 Cyan
7 White
8 Not used
9 Reset to default color

Here’s a summary of some popular options:

tput setaf (set ANSI foreground color)
tput setab (set ANSI background color)
tput bold (for bold text)
tput smul (for underline)
tput sgr0 (reset all attributes)

The below code sets the shell name to bold and green text, then resets formatting afterwards.

PS1="\[$(tput bold; tput setaf 2)\]\s\[$(tput sgr0)\] \$"

Shell functions

You may want to alter the prompt information depending on certain things, such as the return code of the last command or git information.

Here is an example of how to write a bash function that will show your git branch if you are in a git project folder.

parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'

Then you add it into your PS1 prompt string surrounded by backslash-escaped backticks, which will execute the command each time the prompt is drawn.

PS1="\`parse_git_branch\` \$ "

WARNING! There is a performance cost for any functions or processes run when a prompt is redrawn. An example is when users run git status and add this info into a prompt string — this can really slow down the execution of showing the prompt after each command.

Complete example of a custom prompt

Here is a complete working example of the prompt seen at the top of this post. Feel free to copy and modify it to make it your own.

It needs to be added to your shell resource or profile file. If you are using Bash, this is ~/.bashrc.

If your PS1 prompt string is fairly long and complex, you can organise it with variables for each type of formatting and section.

# Add the following to the end of your ~/.bashrc
# print the git branch name if in a git project
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
# set the input prompt symbol
# define text formatting
PROMPT_BOLD="$(tput bold)"
PROMPT_UNDERLINE="$(tput smul)"
PROMPT_FG_GREEN="$(tput setaf 2)"
PROMPT_FG_CYAN="$(tput setaf 6)"
PROMPT_FG_YELLOW="$(tput setaf 3)"
PROMPT_FG_MAGENTA="$(tput setaf 5)"
PROMPT_RESET="$(tput sgr0)"
# save each section prompt section in variable
# set the prompt string using each section variable

Happy Holidays!

You can combine tput with other command line tools, such as figlet http://www.figlet.org/figlet-man.html

echo; tput setaf 1; figlet -c -f roman Happy; tput setaf 2; figlet -c -f roman Holidays; tput sgr0;

Find out more

I thoroughly recommend William Shott’s excellent website on learning linux and shell commands: linuxcommand.org

Its accompanying book explains commands in a way that is easy to follow and try out yourself - The Linux Command Line, which is available as a free download and as a printed book from No Starch Press.

The title of this post is a dedication to the first blog post that helped me customise my terminal and shell — Badassify your terminal and shell — special thanks to Jilles Soeters.