DEV Community

Jordan Kicklighter
Jordan Kicklighter

Posted on • Updated on • Originally published at jwkicklighter.com

Bash Quick Tips

This was originally posted to my blog with some photo examples included.

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!

Bash

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 myip.opendns.com @resolver1.opendns.com`
Enter fullscreen mode Exit fullscreen mode

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 "$@"
}
Enter fullscreen mode Exit fullscreen mode

Usage: mkc [Directory Name]

Git

Git Checkout Search

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.

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:.* ::")
}
Enter fullscreen mode Exit fullscreen mode

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
[alias]
  done = "!f() { git checkout master && git branch -d @{-1}; }; f"
Enter fullscreen mode Exit fullscreen mode

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)'
Enter fullscreen mode Exit fullscreen mode

Usage: gpu

Pretty Git Log Tree

Display the git log in a tree format.

alias gl='git log --all --graph --decorate --oneline --simplify-by-decoration'
Enter fullscreen mode Exit fullscreen mode

Usage: gl

Credit: someone on Twitter several years ago.

Kubernetes

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:

kube_exec [STRING IN POD NAME] [CONTAINER IN POD] [COMMAND]
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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

Latest comments (5)

Collapse
 
kardelio profile image
Benjamin Kadel

This is awesome! Especially love the git branch checkout with fzf....SOOOO good!

Collapse
 
jwkicklighter profile image
Jordan Kicklighter

Right? I was trying to remember a full branch name, but they all started with ticket numbers. Good luck remembering that. So I tried to do a fuzzy tab complete like zsh does with directories. When that didn't work, I immediately thought "wait, don't I have fzf or something?" Bingo

Collapse
 
jpchateau profile image
Jean-Philippe Chateau

Hi! Instead of creating an alias to go in the directory you just created, I suggest using !$ to recall the last argument. Although it does not save a command, it avoids retyping.

Example:
$ mkdir my_directory
$ cd !$

Btw, nice article. Thank you for the tips!

Collapse
 
jwkicklighter profile image
Jordan Kicklighter

Thank you! I knew there was a way to use the previous arg, but I couldn't remember it when I was writing (I guess I could've looked it up πŸ˜…). So thanks for the tip!

I do still like that mkc is shorter than even mkdir because I'm just that lazy.

Collapse
 
jorotenev profile image
Georgi Tenev

Isn't @ the arg received? $_ is the previous arg if I'm not mistaken