从Bash shell将文本转换为字节?

时间:2018-05-16 15:17:01

标签: bash shell encoding command-line utility

如何使用Bash和/或常见的Linux命令行实用程序将文本字符串转换为UTF-8编码的字节?例如,在Python中可以做到:

"Six of one, ½ dozen of the other".encode('utf-8')
b'Six of one, \xc2\xbd dozen of the other'

有没有办法在纯Bash中执行此操作:

STR="Six of one, ½ dozen of the other"
<utility_or_bash_command_here> --encoding='utf-8' $STR
'Six of one, \xc2\xbd dozen of the other'

1 个答案:

答案 0 :(得分:2)

Perl救援!

echo "$STR" | perl -pe 's/([^x\0-\x7f])/"\\x" . sprintf "%x", ord $1/ge'

/e修饰符允许将代码包含在s///替换的替换部分中,在这种情况下,通过ordsprintf转换为十六进制。