Skip to content

Instantly share code, notes, and snippets.

@izabera
Forked from geirha/gist:887e74e74877c7613920
Last active August 29, 2015 14:26
Show Gist options
  • Save izabera/ad1947357a96e51a8559 to your computer and use it in GitHub Desktop.
Save izabera/ad1947357a96e51a8559 to your computer and use it in GitHub Desktop.
Bash urlencode and urldecode
urlencode() {
# urlencode <string>
local LC_ALL=C c i n IFS=
if (( (n = ${#1}) > 30 )); then
printf %s "$1" |
while read -rN1 c; do
if [[ $c = [[:alnum:].~_-] ]]; then printf %s "$c"
else printf %%%02X "'$c"
fi
done
else
for (( i = 0; i < n; i++ )); do
c=${1:i:1}
if [[ $c = [[:alnum:].~_-] ]]; then printf %s "$c"
else printf %%%02X "'$c"
fi
done
fi
}
urldecode() {
# urldecode <string>
printf '%b' "${1//%/\\x}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment