Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Created February 14, 2014 08:35
Show Gist options
  • Save coderofsalvation/8997682 to your computer and use it in GitHub Desktop.
Save coderofsalvation/8997682 to your computer and use it in GitHub Desktop.
applies a raw url encode on the string
# 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