Skip to content

Instantly share code, notes, and snippets.

@varqox
Created June 2, 2025 07:22
Show Gist options
  • Save varqox/70565e32025dd3dfec2cc53542a2bd8f to your computer and use it in GitHub Desktop.
Save varqox/70565e32025dd3dfec2cc53542a2bd8f to your computer and use it in GitHub Desktop.
#!/bin/bash
#set -exuo pipefail
shopt -s expand_aliases
function safe_function {
echo "$@"
echo "sf: ${BASH_LINENO[0]}"
sed -n "${BASH_LINENO[0]},/^}/p" "${BASH_SOURCE[1]}"
}
alias safe_function='
__safe_function_line="${LINENO}"
__safe_function_name="$(sed -n "${__safe_function_line}s/\s*safe_function\s\+\(\(\w\|-\|_\)\+\).*/\1/p" < "${BASH_SOURCE[0]}")" &&
function __safe_function { #'
alias end_safe_function='} &&
eval "function $__safe_function_name {
echo done
}"'
safe_function x-y_z:
echo xyz
echo $@
echo done
end_safe_function
echo declared
declare -f x-y_z
x-y_z
exit 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment