开发者

Replace percent-escaped characters in string (%20, %5B, …) with bash

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.

开发者_开发问答

How can I do that?


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]


Finally, thanks to #bash IRC channel, I found a "not so bad" solution :

echo `echo string%20with%5Bsome%23 | sed 's/%/\\\x/g'`
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜