Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zonesan/5393723e597d2ed3c194cbdc8c8cda23 to your computer and use it in GitHub Desktop.
Save zonesan/5393723e597d2ed3c194cbdc8c8cda23 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