Manually customize bash terminal prompt look

The prompt text that bash interprets and uses is set with the PS1 environment variable and is usually specified in ~/.bashrc. I have been manually tweaking my bash prompt look and feel to make some of my work project use-case tasks easier. The combination of default style changes, and specific prompt elements added for active environment or sourced environments should make terminal-based project management a little easier.

A little searching allowed me to discover this website that allows you to interactively customize the prompt contents and style with common components. https://bash-prompt-generator.org/

Drag and drop prompt components #

For my default setup I landed on the following arrangement for my default prompt.

drag and drop your prompt components

Resulting PS1 #

The website provided an example prompt based on my selections and styling, as well as the equivalent PS1 string to use.

manual prompt output

Copy and paste to the relevant section of ~/.bashrc to make the default. To activate without closing an existing terminal or logging out, you can run exec bash in an exiting terminal.

1
PS1='\[\e[38;5;150m\]\u\[\e[0m\]@\[\e[38;5;74m\]\H\[\e[0m\]:\[\e[38;5;208m\]\w \[\e[0;2m\]\t \[\e[0m\](\[\e[38;5;171;1;4m\]$(git branch 2>/dev/null | grep '"'"'*'"'"' | colrm 1 2)\[\e[0m\])\n\$'

Environment-specific prompts #

Like virtual environments in Python do when the name of the active environment is added, the contents and style of the prompt can be modified to highlight specific information for the user.

I’m using Bash Prompt Generator as a testbed for additional components that will help provide project specific information in the prompt.

Starship #

Starship is a configurable cross-shell tool to style a number of shells’ prompts with tons of pre-designed themes available, and a solid out-of-the-box default too. This is a nice compromise for those who would like a little more style and information in their prompt beyond distro defaults, but don’t necessarily want to design a theme or manually tailor PS1 for specific uses.

It can be installed as easily as the following commands on GNU/Linux similar things:

1
curl -sS https://starship.rs/install.sh | sh

The following ~/.bashrc change is required to enable starship for bash.

1
echo 'eval "$(starship init bash)"' >> ~/.bashrc

There are variations of this configuration for all of the supported shells including Window’s CMD and PowerShell.