笨熊之家

欢迎来到笨熊之家~

0%

为什么zsh在输出后加上高亮符号

记一次有趣的搜索过程

今天在写CSAPP练习题的时候,用到了printf这个函数,仅输出结果而没有在最后加上换行\n,结果ZSH中显示的是输出加上一个高亮的百分号,这让我百思不得其解。

这里先附上当时的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

char is_little_endian()
{
int val = 1;
return *((char *) &val);
}

int main(int argc, char const* argv[])
{
printf("%.2x", is_little_endian());
return 0;
}

然后我就去google了。

尝试过的搜索关键词如下:

  • char* to int why show %
  • c why printf show %
  • c why printf show percent
  • c why printf append a %
  • c why printf print extra %
  • c printf 多出一个%
  • c printf without newline will print % in terminal
  • zsh auto new line after output

可以看出我一开始以为是类型转换导致有部分字符打印不完全的锅,过了一会儿我才想到可能是zsh的bug(不,是feature!),果然就在StackOverflow上找到了相关的问题。

这里引用采纳答案中的一段引用

When a partial line is preserved, by default you will see an inverse+bold character at the end of the partial line: a “%” for a normal user or a “#” for root. If set, the shell parameter PROMPT_EOL_MARK can be used to customize how the end of partial lines are shown.

就是zsh的锅!最后给输出加上换行符就可以看到正常的输出啦~

如果你想在自己的zsh里测试这个的话,考虑依次输入这两个命令并比较输出:

1
2
print -n "This is a test"
print "This is a test"