在字符串使用bash更换%的转义字符(%20%5B,...) [英] Replace percent-escaped characters in string (%20, %5B, …) with bash
本文介绍了在字符串使用bash更换%的转义字符(%20%5B,...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含类似%的转义字符的字符串%20
和%5B
,我想改变到正常的字符,如 \\
为%20
和 [
为%5B
。
I have strings containing percent-escaped characters like %20
and %5B
, and I would like to transform it to "normal" characters like \
for %20
and [
for %5B
.
我怎么能这样做?
推荐答案
在bash的内置的printf有一个特殊的格式说明符(即%B),它将\\点¯x**到相应的值:
The builtin printf in bash has a special format specifier (i.e. %b) which converts \x** to the corresponding value:
$ str='foo%20%5B12%5D'
$ printf "%b\n" "${str//%/\\x}"
foo [12]
这篇关于在字符串使用bash更换%的转义字符(%20%5B,...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文