bash - 从文件中将 unicode 字符作为 utf8 回显
问题描述
当我echo "\U0041"
在终端中运行时,我得到“A”。
但是,当我echo "\U0041"
从文件中example.sh
运行时,请运行它。
bash ./example.sh
或者
sh ./example.sh
我明白了\U0041
。
为什么?如何从文件中回显/打印“A”。
更新:
"$SHELL" --version
->zsh 5.8 (x86_64-apple-darwin20.0)
- 我使用 brew 将 bash 升级到 bash-5.1。
- 当我
echo $'\u0041'
从文件中运行时,我确实看到了“A”
我现在的问题是,当我似乎只能用单引号打印 uf8 但变量需要双引号时,我如何让十六进制代码成为变量?
HEX='0041'
echo $"\u$HEX"
印刷:\u0041
解决方案
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.
推荐阅读
- scala - 通过 SparkStructedStreaming 连接到 ActiveMq 时出现 MqttException
- python - 如何使用 python 在 psql 数据库中创建一个表?
- c - 未能合并文件 /usr/lib/libc_nonshared.a(elf-init.oS) 的目标特定数据
- python-3.x - 在 MacOS 上设置 Python 版本
- azure - 检索多个秘密值密钥保管库
- c - 即使以二进制模式写入文件也会损坏
- sqlite - 类'列表
' 没有实例 getter 'values'。接收方:“_GrowableList”的实例(长度:11)尝试调用:值 - julia - 如何使用古罗比?
- powershell - 将 2 个 CSV 文件与 compare-object 进行比较以获得过时的软件
- javascript - 将 ReactJS、NextJS 配置文件移动到“config”目录而不是根文件夹中