首页 > 解决方案 > 如何在 bash 中对所有字符(特殊和非特殊)进行 url 编码

问题描述

如何对 bash 中的所有字符进行 url 编码?

例如: abcd0123 => %61%62%63%64%30%31%32%33

我在 Linux 中尝试了内置的 urlencode 函数,但它只编码特殊字符,如 (.?)

标签: bashurlencode

解决方案


我不相信这在纯 Bash 中是可能的,但是可以将其他 shell 实用程序串在一起来实现这个结果,例如

urlencode() {
    printf %s "$1" | od -An -tx1 -v -w${#1} | tr ' ' %
}
urlencode abcd0123 # => %61%62%63%64%30%31%32%33

推荐阅读