Last active
December 17, 2024 01:48
-
-
Save tcely/1c8d7477958b081a4c54b761833047cd to your computer and use it in GitHub Desktop.
sh url_decode
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
url_decode() { | |
local arg ; | |
if [ $# -gt 0 ]; then | |
for arg; do | |
printf -- '%s\n' "${arg}" ; | |
done | url_decode ; | |
else | |
check_printf ; | |
printf -- '%b\n' "$(sed -E -e 's/\+/ /g' -e 's/%([0-9a-fA-F]{2})/\\x\1/g')" ; | |
fi ; | |
unset -v arg ; | |
} | |
# busybox sh and bash both worked | |
# dash has a printf built-in that must be avoided | |
# fallback to the printf program when a built-in failed | |
check_printf() { | |
if [ 'Z' != "$(command printf -- '%b\n' '\x5a')" ]; then | |
printf() { /usr/bin/env printf "$@" ; } ; | |
fi ; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment