6

I want to print repeated * characters in zsh. This answer has a solution that works in bash:

printf '*%.0s' {1..50}

However when I run that in zsh, I get this output:

**************************************************%

where the trailing % sign has inverted colors. This is mysterious to me and I want to know why that happens, and how do I avoid it?

Michael Dorst
  • 8,210
  • 11
  • 44
  • 71
  • I am not sure why this is happening, but it seems to be a display thing only. If you write that output to a file, and then `vim` the file, it won't be there. – Mike Furlender Jul 25 '19 at 20:39
  • @MikeFurlender Yes it is. I just tried that and it is in fact there in the file. – Michael Dorst Jul 25 '19 at 20:41
  • @MikeFurlender oh interesting. When you `vim` the file, it's not there, but when you `cat` the file it is. So there must be a character stored, but `vim` isn't showing it. – Michael Dorst Jul 25 '19 at 20:49
  • Possible duplicate of [Why does a cURL request return a percent sign (%) with every request in ZSH?](https://stackoverflow.com/questions/29497038/why-does-a-curl-request-return-a-percent-sign-with-every-request-in-zsh) – melpomene Jul 25 '19 at 20:57
  • @MichaelDorst It is not the case that there is a character but `vim` isn't showing it. Try this: `printf '*%.0s' {1..1} | wc -c` - the output is `1` – Mike Furlender Aug 01 '19 at 14:27
  • @MikeFurlender yes apparently zsh prints `%` with inverted colors and a newline in place of nothing, to show that there is no newline, but still allow the prompt to display correctly on the next line. Now that I know what it's doing, I'm a big fan of that feature. – Michael Dorst Aug 02 '19 at 18:49

1 Answers1

10

That's not a character, it's the lack of a character.

If the last line of output is not terminated (i.e. does not end with a newline character, \n), zsh shows a reverse-video % sign. See http://zsh.sourceforge.net/Doc/Release/Options.html#Prompting.

The fix is to just output a terminating newline:

printf '*%.0s' {1..50}; echo
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • So `zsh` inserts `%` and a newline so that the prompt doesn't get printed at the end of the previous line, like it does in bash? That's actually really cool, I'm glad I learned this. – Michael Dorst Jul 25 '19 at 21:40