一、百分号编码字符串
(1)、运行截图
(2)、示例函数
urlencode() {
local LC_ALL=C
for (( i = 0; i < ${#1}; i++ )); do
: "${1:i:1}"
case "$_" in
[a-zA-Z0-9.~_-])
printf '%s' "$_"
;;
*)
printf '%%%02X' "'$_"
;;
esac
done
printf '\n'
}
(3)、示例用法
$ urlencode "https://github.com/dylanaraps/pure-bash-bible"
https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible
二、解码用百分比编码的字符串
(1)、运行截图
(2)、示例函数
urldecode() {
: "${1//+/ }"
printf '%b\n' "${_//%/\\x}"
}
(3)、示例用法
$ urldecode "https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible"
https://github.com/dylanaraps/pure-bash-bible