Skip to content

Instantly share code, notes, and snippets.

@zbentley
Created March 30, 2018 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zbentley/3832f31d33fb476c89bd2660b086b148 to your computer and use it in GitHub Desktop.
Save zbentley/3832f31d33fb476c89bd2660b086b148 to your computer and use it in GitHub Desktop.
Bash function wrapping with minimal eval
#!/usr/bin/env bash
wrap() {
local old_name="${1:?get fuct}"
local new_name="${2:?get fuct}"
_temp_function() {
echo "wrapping the old one!"
_old_function_call_placeholder param
echo "after calling the old one!"
};
local new_body=$( declare -f "_temp_function" | tail -n +2 )
eval "$(printf "%s()\n%s" $new_name "${new_body/_old_function_call_placeholder/$old_name}")"
unset _temp_function
}
foo() {
echo "this is foo!"
}
# define "bar" as wrapping "foo"
wrap foo bar
bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment