Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
URL Encode with Shell Script
#!/usr/bin/env bash
#
# Credits to Meleu @ https://meleu.sh/urlencode/
# Usage: urlencode "#" => will output "%23"
urlencode() {
local LC_ALL=C
local string="$*"
local length="${#string}"
local char
for (( i = 0; i < length; i++ )); do
char="${string:i:1}"
if [[ "$char" == [a-zA-Z0-9.~_-] ]]; then
printf "$char"
else
printf '%%%02X' "'$char"
fi
done
printf '\n' # opcional
}
urlencode "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment