Created
February 14, 2014 08:35
-
-
Save coderofsalvation/8997682 to your computer and use it in GitHub Desktop.
applies a raw url encode on the string
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
# applies a raw url encode on the string | |
# usage: urlencode "foo foo bar" <-- outputs: foo%3dfoo%20bar | |
# @param string stringtoencode | |
urlencode() { | |
local string="${1}"; local strlen=${#string}; local encoded="" | |
for (( pos=0 ; pos<strlen ; pos++ )); do | |
c=${string:$pos:1} | |
case "$c" in | |
[-_.~a-zA-Z0-9] ) o="${c}" ;; | |
* ) printf -v o '%%%02x' "'$c" | |
esac | |
encoded+="${o}" | |
done | |
echo "${encoded}" # You can either set a return variable (FASTER) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment