A few ideas for personalising your cli. Make it your own.


We are going to edit our PS1 env value in bash. This is the output right before your command.

The part before echo is our PS1 prompt
PS1 is the primary prompt which is displayed before each command, thus it is the one most people customize.
- https://wiki.archlinux.org/index.php/Bash/Prompt_customization

So how do we do this ?

We’ll slowly build our PS1 with the parts we want.

Directory name

We’ll start with a simple directory name. In PS1 this is done by \W

edit your ~/.bash_profile , and add the following:

export PS1='\W \$- '

Save and exit that, then run source ~/.bash_profile and you’ll see your directory name listed.

Working directory is now

Git branch of current Directory

We’ll need a function which gets the current Git branch and throws that in our PS1.

This function has a little branch symbol to show that it’s git, and then the git branch.

Add it to your ~/.bash_profile and then add to your PS1

__get_git_branch() {
    GIT_SYMBOL=$'\xE2\x8E\x87 '
    echo "(${GIT_SYMBOL} $(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'))"
}

export PS1="\W \$(__get_git_branch) \$ "

Save and exit, and then run source ~/.bash_profile again to reload our PS1 value.

Now our git branch is there as well, and it’s live.

Kubernetes Context

This one has saved me countless times from applying to the wrong environment

This function will get our current Kubernetes contexts, and then we can add this to our PS1 again.

The function does look a bit complex, but it does add a piece for the namespace as well, so you’re not confused when you see no resources found.

__get_kubernetes_context()
{
    # Get current context
    CONTEXT=$(kubectl config current-context)
    # A nice little icon for Kubernetes          
    KUBE_SYMBOL=$'\xE2\x8E\x88 '
    if [ -n "$CONTEXT" ]; then
        NAMESPACE=$(kubectl config view --minify --output 'jsonpath={..namespace}')
        if [ -n "$NAMESPACE" ]; then
            echo "(${KUBE_SYMBOL} ${CONTEXT} :: ${NAMESPACE})"
        else
            echo "(${KUBE_SYMBOL} ${CONTEXT}:None)"
        fi
    fi
}
export PS1="\W \$(__get_git_branch) \$(__get_kubernetes_context) \$ "

Save and exit, and then run source ~/.bash_profile to reload the PS1 value.

Now you can see you current context & current namespace before your command

It’s still looking a bit bland though.

Let’s add some colour to make it pretty.

In bash we can echo certain codes for colour, and everything after that value will be in the defined colour.

Your can find more information here: https://www.shellhacks.com/bash-colors/

So let’s define some colours. in ~/.bash_profile add a few colour codes, and a none-colour code to remove your colours afterwards, otherwise your whole terminal will end up being all weird.

# Remove all colour attributes with 00m
NORMAL="\[\033[00m\]"

BLUE="\[\033[01;34m\]"
YELLOW="\[\e[1;33m\]"
GREEN="\[\e[1;32m\]"

Now in your PS1 add the colours before the functions to change their colour.

# Add ${COLOUR} before output to change it's 
# Add ${NORMAL} to remove colour before actual command
export PS1="${BLUE}\W ${GREEN}\$(__get_git_branch) ${YELLOW}\$(__get_kubernetes_context)${NORMAL} \$ "

Save & Exit, and then run source ~/.bash_profile .

Bam. No more wrong branch, wrong context errors

Pro Tip

If you have ever accidentally run Kubernetes apply’s against the wrong cluster, you can also add alias per context.

Disclaimer: This was stolen from a previous colleague and I will take no credit for the idea.

With kubectl you can define which context to run against while using kubectl:

$ kubectl get pods --context=app-cluster

How about creating an alias for this , and using that instead of kubectl ?

alias ku-app="kubectl --context=app-cluster"
alias ku-ops="kubectl --context=ops-cluster"

Save & exit, run source ~/.bash_profile

No more mistakes. Enjoy !