Created
April 28, 2021 12:36
-
-
Save kirillsulim/fb9f04a9c80651d95c9fc0a92627d814 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
trap "exit 1" TERM | |
export TOP_PID=$$ | |
function error() { | |
echo "Error: $1" | |
kill -s TERM $TOP_PID | |
} >&2 | |
HDIR='${HOME}' | |
HHDIR='${HDIR}' | |
recursive_eval () { | |
local expr="$1" | |
local loop_guard=0 | |
while [[ $expr =~ \$(\{[A-Za-z0-9_]+\}|[A-Za-z0-9_]+) ]]; do | |
((loop_guard++)) | |
if [[ "${loop_guard}" -gt 2 ]]; then | |
echo $(error "Possible infinite loop, aborting.") | |
exit 1 | |
fi | |
expr=$(eval echo "${expr}") | |
done | |
echo "${expr}" | |
} | |
echo $(recursive_eval 'abc') | |
echo $(recursive_eval 'My home is: ${HOME}') | |
echo $(recursive_eval 'My home is: $HDIR') | |
echo $(recursive_eval 'My home is: ${HHDIR}') | |
echo "Success!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment