首页 > 解决方案 > 从文件中将 unicode 字符作为 utf8 回显

问题描述

当我echo "\U0041"在终端中运行时,我得到“A”。

但是,当我echo "\U0041"从文件中example.sh运行时,请运行它。

bash ./example.sh或者 sh ./example.sh

我明白了\U0041

为什么?如何从文件中回显/打印“A”。


更新

我现在的问题是,当我似乎只能用单引号打印 uf8 但变量需要双引号时,我如何让十六进制代码成为变量?

HEX='0041'
echo $"\u$HEX"

印刷:\u0041

标签: bashmacosshellunicode

解决方案


You're using zsh as the shell in your terminal. Its version of echo understands \uXXXX escapes. So does the echo that's built in to modern versions of bash (If using echo -e or if the xdg_echo shell option is enabled), but the ancient version that comes with Mac OS is too old to have it.


推荐阅读