Bash

URL-Encode and URL-Decode

admin by @admin ADMIN
Jun 13, 2026
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Encode and decode URL-safe strings entirely in bash — no python or external tool required. Useful in pure-shell deployment scripts that need to build query strings.
Bash
Raw
urlencode() {
    local s="$1" out=""
    local i c
    for (( i = 0; i < ${#s}; i++ )); do
        c="${s:i:1}"
        case "$c" in
            [a-zA-Z0-9._~-]) out+="$c" ;;
            *) printf -v hex '%%%02X' "'$c"
               out+="$hex" ;;
        esac
    done
    printf '%s' "$out"
}

urldecode() {
    local s="${1//+/ }"
    printf '%b' "${s//%/\\x}"
}

urlencode "hello world & friends"     # hello%20world%20%26%20friends
urldecode "hello%20world%20%26%20friends"   # hello world & friends
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.