Skip to content

Instantly share code, notes, and snippets.

@geirha
Forked from cdown/gist:1163649
Last active July 25, 2024 03:11
Show Gist options
  • Save geirha/887e74e74877c7613920 to your computer and use it in GitHub Desktop.
Save geirha/887e74e74877c7613920 to your computer and use it in GitHub Desktop.
Bash urlencode and urldecode
urlencode() {
# urlencode <string>
local LC_ALL=C c i n
for (( i = 0, n = ${#1}; i < n; i++ )); do
c=${1:i:1}
case $c in
[[:alnum:].~_-]) printf %s "$c" ;;
*) printf %%%02X "'$c" ;;
esac
done
}
urldecode() {
# urldecode <string>
local s
s=${1//\\/\\\\}
s=${s//+/ }
printf %b "${s//'%'/\\x}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment