Replace percent-escaped characters in string ( , [

2019-03-29 14:56发布

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?

2条回答
Animai°情兽
2楼-- · 2019-03-29 15:43

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]
查看更多
霸刀☆藐视天下
3楼-- · 2019-03-29 15:47

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

echo `echo string%20with%5Bsome%23 | sed 's/%/\\\x/g'`
查看更多
登录 后发表回答