Created
March 30, 2018 15:54
-
-
Save zbentley/3832f31d33fb476c89bd2660b086b148 to your computer and use it in GitHub Desktop.
Bash function wrapping with minimal eval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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