Skip to content

Instantly share code, notes, and snippets.

@artnez
Created May 6, 2013 04:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artnez/5523319 to your computer and use it in GitHub Desktop.
Save artnez/5523319 to your computer and use it in GitHub Desktop.
Monkey patching a function in bash.
#! /usr/bin/env bash
#
# Monkey patching a function in bash. Why.
patch() {
new_guy=$(declare -f $1)
new_guy=${new_guy##$1*\{}
new_guy=${new_guy/\}/}
old_guy=$(declare -f $2)
old_guy=${old_guy/$2*{/}
old_guy=${old_guy/\}/}
eval "$2() { $new_guy $old_guy }"
}
search() {
echo "do the original thing"
}
sneaky() {
echo "wait, do this first"
}
patch sneaky search
search
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment