Skip to content

Instantly share code, notes, and snippets.

@aljex aljex/urlencode.sh
Created Mar 27, 2019

Embed
What would you like to do?
Url-encode in pure bash as efficient as possible
#!/bin/bash
# urlencode in pure bash without externals
# no backticks or forking, all in-memory ops
# allow bash-isms
urlencx () {
local LANG=C i c e
for ((i=0;i<${#x};i++)); do
c=${x:$i:1}
[[ "$c" =~ [a-zA-Z0-9\.\~\_\-] ]] || printf -v c '%%%02X' "'$c"
e+=$c
done
x=$e
}
printf -v x "This'll be fun\!\n*Won't* \"it\"?"
echo "BEFORE: x=\"${x}\""
urlencx
echo "AFTER: x=\"${x}\""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.