我们有一些从 Windows 中的 Git Bash (MINGW64) 运行的 Groovy 脚本。 某些脚本会打印项目符号字符•(或类似字符)。 为了使它工作,我们设置这个变量:
导出 LC_ALL=en_US.UTF-8
但是,对于某些人来说,这还不够。它的控制台打印 ГÇó
而不是 •
。
知道如何使其正确打印以及为什么即使在设置 LC_ALL 变量后仍要打印它吗?
更新
关键部分是 Groovy 脚本的输出打印不正确,但普通 bash 脚本没有问题。
最佳答案
查询系统区域设置使用的当前字符映射 locale charmap
的示例,并使用 recode 过滤输出以使用兼容的字符映射进行渲染:
#!/usr/bin/env sh
cat <<EOF | recode -qf "UTF-8...$(locale charmap)"
• These are
• UTF-8 bullets in source
• But it can gracefully degrade with recode
EOF
使用 charmap=ISO-8859-1
它呈现为:
o These are
o UTF-8 bullets in source
o But it can gracefully degrade with recode
使用 iconv
代替 recode
的替代方法,结果可能会更好。
#!/usr/bin/env sh
cat <<EOF | iconv -f 'UTF-8' -t "$(locale charmap)//TRANSLIT"
• These are
• UTF-8 bullets followed by a non-breaking space in source
• But it can gracefully degrade with iconv
• Europe's currency sign is € for Euro.
EOF
带有 fr_FR.iso-8859-15@Euro
语言环境的 iconv
输出:
o These are
o UTF-8 bullets followed by a non-breaking space in source
o But it can gracefully degrade with iconv
o Europe's currency sign is € for Euro.
关于Bash:即使将区域设置设置为 UTF-8 后也会出现奇怪的字符: "•"打印为 "ΓÇó",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64253633/