Dynamic Bash Command Prompt Highlighting

December 2017   Love

In this project, I created dynamic coloring for my bash command prompt. There are a variety of shells for the command line, however the code presented below is designed for bash Unix shell, the default login shell used for Apple's macOS. While there are many third party resources that make it easy to change the color of your command prompt, the majority of them do not support dynamic prompt coloring - that is, coloring that changes to facilitate the reading of your working directory. This post goes over how to add dynamic command prompt coloring to your bash shell and discusses a few ways to customize your prompt appearance!

Overview

The appearance of your bash command prompt can be changed by customizing the $PS1 variable in your .bashrc file.  There are resources on the internet that help people create cool coloring for the command prompt, such as ezprompt.net, however, sites like these provide static coloring that does not change based on the user's current working directory.  The code presented bellow allows the user to work more efficiently by dynamically coloring the current working directory, making it easy for the user to know exactly where he or she is in a filesystem.  The image below shows and example of this.

Figure 1: Example of bash command prompt dynamic coloring

Changes to .bashrc

In your .bashrc file in your home directory, paste the following code:

# Add dynamic command prompt coloring that highlights current working directory
function prompt_command {
	# Define parts of command prompt (end variables in x so they do not over-write pre-existing variables)
	ATx='\[\e[2m\]\[\e[38;5;33m\]@\[\e[m\]\[\e[m\]'
	HOSTx='\[\e[38;5;33m\]\h:\[\e[m\] '
	USERx='\[\e[1m\]\[\e[38;5;74m\]\u\[\e[m\]\[\e[m\]'
	DIRx='\[\e[1m\]\[\e[38;5;74m\]/\W\[\e[m\]\[\e[m\] '
	MONEYx='\[\e[38;5;33m\]$\[\e[m\] '
	# Path to the parent of the current working directory
	parentdir="$(dirname "$(dirs +0)")"
	# Coloring logic
	if [[ $parentdir == "." ]]
	then
		parentdir=""
	elif [[ $parentdir == "/" ]]
	then
		parentdir=""
		DIRx='\[\e[1m\]\[\e[38;5;74m\]$(dirs +0)\[\e[m\]\[\e[m\] '
	fi
	PARENTx='\[\e[34m\]$parentdir\[\e[m\]'
	# Add command prompt coloring
	export PS1=$USERx$ATx$HOSTx$PARENTx$DIRx$MONEYx
}
# PROMPT_COMMAND is always executed before the prompt
export PROMPT_COMMAND=prompt_command

Note that all the variable end with an "x".  This is to prevent re-assigning variables that may already be in use by the system (i.e. the $USER variable).  One may change the colors of the respective elements by embracing the different elements in different colors.  This site may be of use if you are interested in changing the colors/style of the prompt.

Changes to .bash_profile

By default, interactive shells on OS X are login shells.  This means that your .bash_profile file is executed when you login to the shell.  Thus, in order for the command prompt highlighting to be called, you must source the .bashrc file.  You can do this by adding the following lines to your .bash_profile file (again located in your home directory):

# source .bashrc for command prompt coloring
source ~/.bashrc

After doing this, be sure to restart the shell, or run the command: source .bash_profile in your bash shell/terminal window, and the text coloring should take effect!

Back to Henry Back to all posts