Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Created April 10, 2016 00:28
Show Gist options
  • Save magnetikonline/b9f32aaa31d7dad6dd6cdd0babc4414e to your computer and use it in GitHub Desktop.
Save magnetikonline/b9f32aaa31d7dad6dd6cdd0babc4414e to your computer and use it in GitHub Desktop.
Pure Bash urlencode function.
#!/bin/bash
function URLEncode {
local dataLength="${#1}"
local index
for ((index = 0;index < dataLength;index++)); do
local char="${1:index:1}"
case $char in
[a-zA-Z0-9.~_-])
printf "$char"
;;
*)
printf "%%%02X" "'$char"
;;
esac
done
}
encoded=$(URLEncode "my\value\will!be&encoded")
echo "$encoded"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment