URL encoding a string in bash script

我与影子孤独终老i 提交于 2019-11-30 13:39:40

You want $MESSAGE to be in double-quotes, so the shell won't split it into separate words:

ENCODEDMESSAGE=$(php -r "echo urlencode(\"$MESSAGE\");")

On CentOS, no extra package needed:

python -c "import urllib;print urllib.quote(raw_input())" <<< "$message"

Extending Rockallite's very helpful answer for Python 3 and multiline input from a file (this time on Ubuntu, but that shouldn't matter):

cat any.txt | python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.stdin.read()))"

Pure Bash way:

URL='rom%C3%A2ntico'
echo -e "${URL//%/\\x}"

echoes:

romântico

'C3 A2' is 'â' in utf8 hex

utf8 list: http://www.utf8-chartable.de/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!