The bash prompt can be adjusted to ones needs, see this manual page. For example, you can display the current user, directory and time. The Ubuntu default prompt looks like this:

PS1='${debian_chroot:+($debian_chroot)}[\033[01;32m]\u@\h[\033[00m]:[\033[01;34m]\w[\033[00m]\$ '

It is also possible to add the current kubernetes context and the current git branch to the bash prompt, with colors and all:

To prevent a very long bash prompt PS1 string, we first define some colors and functions, and then create the PS1 variable from this:

BLACK="[$(tput setaf 0)]"
DARK_RED="[$(tput setaf 1)]"
DARK_GREEN="[$(tput setaf 2)]"
DARK_YELLOW="[$(tput setaf 3)]"
DARK_BLUE="[$(tput setaf 4)]"
DARK_PURPLE="[$(tput setaf 5)]"
DARK_CYAN="[$(tput setaf 6)]"
LIGHT_GREY="[$(tput setaf 7)]"
DARK_GREY="[$(tput setaf 8)]"
LIGHT_RED="[$(tput setaf 9)]"
LIGHT_GREEN="[$(tput setaf 10)]"
LIGHT_YELLOW="[$(tput setaf 11)]"
LIGHT_BLUE="[$(tput setaf 12)]"
LIGHT_PURPLE="[$(tput setaf 13)]"
LIGHT_CYAN="[$(tput setaf 14)]"
WHITE="[$(tput setaf 15)]"
RESET="[$(tput sgr0)]"

function git_branch() {
  git branch 2> /dev/null | sed -e '/^[^]/d' -e 's/ (.*)/\1/'
}
function k8s_context() {
  context=$(kubectl config current-context 2> /dev/null)
  if [[ $? -eq 0 ]] ; then echo -e "${context}"; fi
}

K8S_CONTEXT="${LIGHT_YELLOW}\$(k8s_context)${RESET}"
USER_AT_HOST="${LIGHT_GREEN}\u${DARK_GREEN}@${LIGHT_GREEN}\h${RESET}"
DIRECTORY="${LIGHT_CYAN}\w${RESET}"
GIT_BRANCH="${LIGHT_PURPLE}\$(git_branch)${RESET}"

PS1="${K8S_CONTEXT}:${USER_AT_HOST}:${DIRECTORY}:${GIT_BRANCH}$ "
Bash prompt with kubernetes context and git branch
Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *