wiki/notebook/cookbook.bash.percent-encoding.org
Gabriel Arazas b088086b06 Merge evergreen notes into the notebook
Now, it's all under the notebook umbrella. Seems to be appropriate as it
is just my notes after all.

I also updated some notes from there. I didn't keep track of what it is
this time. Something about more learning notes extracted from my
"Learning how to learn" course notes and then some. Lack of time and
hurriness just makes it difficult to track but it should be under
version control already.
2021-07-21 16:28:07 +08:00

1.6 KiB

Percent encoding in Bash

Percent-encoding is commonly used in URLs. Pretty nice especially when used with spaces.

Based from this implementation. It was modified to be closer to the output from Python urlencode.

function urlencode {
    local msg=$1
    local length="${#1}"

    for (( i = 0; i < length; i++ )); do
        local ch="${msg:i:1}"
        case $ch in
            [a-zA-Z0-9.~_-])
                printf "%c" $ch;;
            ,*)
                printf '%s' "$ch" | xxd -plain -cols 1 | {
                    while read hex; do
                        printf "%%%s" $hex
                    done
                };;
        esac
    done
    printf "\n"
}

urlencode "El Doggo"
urlencode "El Niño"
urlencode "Whoa there, sonny!"
ElDoggo
ElNiÃo
Whoatheresonny

To decode percent-encoded strings:

function urldecode {
    # urldecode <string>

    # Replace all pluses with spaces since it usually represents it in URLs.
    local url_encoded="${1//+/ }"

    # Replace all percent sign with '\x' and print the message with byte interpretation.
    printf '%b\n' "${url_encoded//%/\\x}"
}

urldecode "El%20Doggo"
urldecode "%e9%bd%8b%e8%97%a4%e3%83%bbB%e3%83%bb%e6%a5%b5%e3%83%bb%e5%b0%86%e5%97%a3"
El Doggo
齋藤・ B ・極・将嗣