Skip to content

Instantly share code, notes, and snippets.

@omkensey
Last active September 29, 2025 15:15
Show Gist options
  • Select an option

  • Save omkensey/6034b871814dcb36d569fa9312ba634f to your computer and use it in GitHub Desktop.

Select an option

Save omkensey/6034b871814dcb36d569fa9312ba634f to your computer and use it in GitHub Desktop.
Snippet to add k8s and git context to bash prompt when relevant
OLDPROMPT="$PS1"
PROMPT_KUBECONTEXT=""
PROMPT_GIT=""
PROMPT_AWS=""
PROMPT_LINE=""
prompt_set() {
if ! [[ -z "$KUBECONFIG" ]]; then
PROMPT_KUBECONTEXT="k8s:$(kubectl config get-contexts --no-headers $(kubectl config current-context 2>/dev/null) 2>/dev/null | awk '{print $3":"$4}') "
# | awk '{print substr($2,1,13)"..."substr($2,length($2)-19)}')"
fi
if [[ $(env | grep -E '^AWS_') ]]; then
PROMPT_AWS="aws:{ profile:${AWS_PROFILE:-"default"} region:${AWS_REGION:-$(aws configure get region 2>/dev/null)} }"
fi
GIT_BRANCH=$(git branch 2>/dev/null | grep -e '\* ' | sed 's/^..\(.*\)/{\1}/')
PROMPT_NEW=$(echo "$OLDPROMPT" | sed -e 's/\\u/\\u'${GIT_BRANCH}'/')
if ! [[ -z "$PROMPT_KUBECONTEXT""$PROMPT_AWS" ]]; then
PROMPT_HEADER="$PROMPT_KUBECONTEXT""$PROMPT_AWS""\n"
fi
PS1="$PROMPT_HEADER""$PROMPT_NEW"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment