Skip to content

Instantly share code, notes, and snippets.

@tcely
Last active December 17, 2024 01:48
Show Gist options
  • Save tcely/1c8d7477958b081a4c54b761833047cd to your computer and use it in GitHub Desktop.
Save tcely/1c8d7477958b081a4c54b761833047cd to your computer and use it in GitHub Desktop.
sh url_decode
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