Skip to content

Instantly share code, notes, and snippets.

@chiuki
Last active October 25, 2023 18:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiuki/3430887 to your computer and use it in GitHub Desktop.
Save chiuki/3430887 to your computer and use it in GitHub Desktop.
Bash prompt with time, path, git branch and exit status
function prompt_command {
exitstatus="$?"
BOLD="\[\033[1m\]"
RED="\[\033[1;31m\]"
GREEN="\[\e[32;1m\]"
BLUE="\[\e[34;1m\]"
PURPLE="\[\e[35;1m\]"
CYAN="\[\e[36;1m\]"
OFF="\[\033[m\]"
time=`date +"%H:%M"`
branch=`git symbolic-ref HEAD 2> /dev/null | sed -e 's/refs\/heads\///g'`
if [ ! -z ${branch} ]; then
if [ ${branch} == "master" ]; then
branch=`echo " ${CYAN}${branch}"`
else
branch=`echo " ${PURPLE}${branch}"`
fi
fi
changes=`git status -s 2> /dev/null | \
wc -l | sed -e 's/ *//'`
if [ ${changes} -eq 0 ]; then
dirty=""
else
dirty="${RED}*${OFF}"
fi
prompt="\u@\h ${CYAN}${time}${OFF} \
${BLUE}\w${OFF}${PURPLE}${branch}${OFF}${dirty}"
if [ ${exitstatus} -eq 0 ]; then
PS1="${prompt} ${GREEN}\\$ ${OFF}"
else
PS1="${prompt} ${RED}\\$ ${OFF}"
fi
PS2="${BOLD}>${OFF} "
dir=`pwd`
title=`basename ${dir}`
echo -n -e "\033]0;${title}\007"
}
PROMPT_COMMAND=prompt_command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment