Skip to content

Instantly share code, notes, and snippets.

@geirha
Forked from cdown/gist:1163649
Last active October 23, 2020 01:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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