Urlencode and decode from the command line with bash

If you want a native bash solution to urlencode and urldecode, put this in your .bashrc

urlencode() {
    # urlencode <string>

    local length="${#1}"
    for (( i = 0; i < length; i++ )); do
        local c="${1:i:1}"
        case $c in
            [a-zA-Z0-9.~_-]) printf "$c" ;;
            *) printf '%s' "$c" | xxd -p -c1 |
                   while read c; do printf '%%%s' "$c"; done ;;

urldecode() {
    # urldecode <string>

    local url_encoded="${1//+/ }"
    printf '%b' "${url_encoded//%/\\x}"

If you want to use this from xargs, you'll need to export the function via:

export -f urlencode
unix unix-urlencode

Edit on github
comments powered by Disqus
Click me