Bash Quick Tips

Cover image

This is a collection of bash aliases/functions and git aliases that make my life a little easier and save me keystrokes. Hopefully you find something useful in here, let me know what tips you have!


Aliases and functions for bash (although technically I use zsh). Some are mine, and some were collected from the internet. Credit is provided where relevant.

For organizational purposes, I have my aliases in .zshrc and most of the functions in a separate file callked .zsh_functions that is included near the top of my .zshrc. Some functions are in separate bash files that are available in my path, and I am in the process of moving most functions into there to lower the amount of functions zsh has to load into memory at start.

Current IP

Get your current external IP address.

alias ip=`dig +short`

Usage: ip

Credit: Linux Training Academy

Make Directory and Go There

Pretty straightforward, this makes a new directory and does a cd into it, saving a command and saving retyping.

mkc () {
  mkdir -p "$@" && cd "$@"

Usage: mkc [Directory Name]


This function opens an fzf fuzzy-search prompt for your git branches, allowing you to find the branch you want with fuzzy search. Since the zsh alias for git checkout is gco, adding the s for "search" made sense to me.

git checkout search screenshot

Note: this requires fzf to be installed and in your path.

gcos () {
  git fetch
  local branches branch
  branches=$(git branch -a) &&
  branch=$(echo "$branches" | fzf +s +m -e) &&
  git checkout $(echo "$branch" | sed "s:.* remotes/origin/::" | sed "s:.* ::")

Usage: Type gcos, hit enter, and then start typing your branch name

Credit: Christoffer Skeppstedt's post on Coderwall

Done with Branch

On projects using Pull Requests/Merge Requests heavily, I found myself constantly clearing out old branches after they were finished. This git alias will checkout master and delete the branch you were just using, but only if it has been merged somewhere. So if it's been pushed to a remote branch or merged to another local branch, it will be removed.

# in .gitconfig
  done = "!f() { git checkout master && git branch -d @{-1}; }; f"

Usage: Type git done, hit enter, and be transported to master while your old branch is annhilated (from your machine only)

Push New Branch

On those same projects, there end up being many branches made locally that need to be pushed to a remote. This bash alias pushes your current branch to origin, creating a branch with the same name as your local branch. (I used gpu because gp is the zsh alias for git push. So I think "git push upload" in my head.)

alias gpu='git push -u origin $(git rev-parse --abbrev-ref HEAD)'

Usage: gpu

Pretty Git Log Tree

Display the git log in a tree format.

pretty git log tree screenshot

alias gl='git log --all --graph --decorate --oneline --simplify-by-decoration'

Usage: gl

Credit: someone on Twitter several years ago.


Execute Commands in Pods

For some of my work, I will have a Kubernetes cluster with debug pods for developers to connect to. I was getting annoyed by having to use kubectl get pods to find the current pod name when I knew that some part would always be consistent (e.g. maybe the word "debug" is present).

This function is used like so:


So for example, if we have a Kubernetes pod called app-debug-pod-12345 with a container inside called rails, the command kube_exec debug rails bash will start a bash session in the rails container running in the first pod that is found containing the string debug.

kube_exec () {
  exec_pod=`kubectl get pods --field-selector 'status.phase!=Failed' | grep ${1} | cut -d" " -f 1 | head -1`
  echo "Executing ${2} in ${exec_pod} at `kubectl config view -o=jsonpath='{.current-context}'`"
  echo ""

  kubectl exec $exec_pod -c $2 -it $3
  unset exec_pod

Usage: kube_exec [POD NAME] [CONTAINER] [COMMAND]