-
-
Save geirha/887e74e74877c7613920 to your computer and use it in GitHub Desktop.
Bash urlencode and urldecode
This file contains 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
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