Skip to content

Instantly share code, notes, and snippets.

@asolera
Created March 19, 2021 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asolera/0b68431ae889a1b01e07605decf42a13 to your computer and use it in GitHub Desktop.
Save asolera/0b68431ae889a1b01e07605decf42a13 to your computer and use it in GitHub Desktop.
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