Too*_*rot 6 bash string unicode printf
我尝试用 bash 的 printf 填充 Unicode 字符串,并看到了这一点,而
\nprintf "%2s" a\nRun Code Online (Sandbox Code Playgroud)\n产生预期的“a”,
\nUnicode 变体
\nprintf "%2s" \xc3\xa4 \nRun Code Online (Sandbox Code Playgroud)\n产生令人惊讶的未填充的“\xc3\xa4”。(zsh 给出了预期的结果。)
\n是什么原因造成的;我应该如何在 bash 中填充 Unicode 字符串?
\nbash 行为正确并且 C 程序
\n#include <stdio.h>\nmain()\n{\n char foo[] = "\xc3\xa4";\n\n printf("%2s\\n", foo);\n}\nRun Code Online (Sandbox Code Playgroud)\n行为相同。
\n这是因为 %s 指的是面向字节的字符串,而 UTF-8 中的 '\xc3\xa4' 结果为 2 个字节。
\n据我测试,其他 shell 都没有行为不正确。
\n您期望的结果可以通过以下方式看到:
\nprintf '%2S\\n' \xc3\xa4\nRun Code Online (Sandbox Code Playgroud)\n但我测试的任何 shell 都不支持这一点。
\n