Skip to content

Instantly share code, notes, and snippets.

Last active June 23, 2022 19:13
What would you like to do?
Sample code for dynamic bash prompt that shows exit codes.
# default macOS prompt is: \h:\W \u\$
# assemble the prompt string PS1
# inspired from:
function __build_prompt {
local EXIT="$?" # store current exit code
# define some colors
local RESET='\[\e[0m\]'
local RED='\[\e[0;31m\]'
local GREEN='\[\e[0;32m\]'
local BOLD_GRAY='\[\e[1;30m\]'
# longer list of codes here:
# start with an empty PS1
if [[ $EXIT -eq 0 ]]; then
PS1+="${GREEN}√${RESET} " # Add green for success
PS1+="${RED}?️️️${EXIT}${RESET} " # Add red if exit code non 0
# this is the default prompt for
PS1+="${BOLD_GRAY}\W ${RESET}\$ "
# set the prompt command
# include previous values to maintain Apple Terminal support (window title path and sessions)
# this is explained in /etc/bashrc_Apple_Terminal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment