From aa490304efc4f5c02efda317ffe2a5a7cf1bd30b Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Mon, 18 Jun 2018 09:25:38 +1000 Subject: [PATCH] Added method of getting function names. --- README.md | 11 ++++++++++- test.sh | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 58e31c8..c945c93 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ scripts and not full blown utilities. * [Check if a program is in the user's PATH.](#check-if-a-program-is-in-the-users-path) * [Get the current date using `strftime`.](#get-the-current-date-using-strftime) * [Progress bars.](#progress-bars) + * [Get the list of functions from your script.](#get-the-list-of-functions-from-your-script) * [Bypass shell aliases.](#bypass-shell-aliases) * [Bypass shell functions.](#bypass-shell-functions) @@ -754,7 +755,6 @@ done shopt -u globstar ``` - # File handling **CAVEAT:** `bash` doesn't handle binary data properly in versions `< 4.4`. @@ -1520,6 +1520,15 @@ done printf '\n' ``` +## Get the list of functions from your script. + +```sh +get_functions() { + # Usage: get_functions + IFS=$'\n' read -d "" -ra functions < <(declare -F) + printf '%s\n' "${functions[@]//declare -f }" +} +``` ## Bypass shell aliases. diff --git a/test.sh b/test.sh index caadc6a..c76604b 100755 --- a/test.sh +++ b/test.sh @@ -139,6 +139,11 @@ test_bar() { assert_equals "${result//$'\r'}" "[----- ]" } +test_get_functions() { + IFS=$'\n' read -d "" -ra functions < <(get_functions) + assert_equals "${functions[0]}" "assert_equals" +} + assert_equals() { if [[ "$1" == "$2" ]]; then ((pass+=1))