Why does a cURL request return a percent sign (%) with every request in ZSH?

▼魔方 西西 提交于 2019-11-29 13:12:14

This is a zsh feature that prints a percent-and-newline after a command completes if that command does not already include a newline at the end of its output. If zsh did not do this, you would either not ever notice the fact that the command didn't print a newline - or you'd see zsh's command prompt not start on the margin and think it was a bug in zsh.

Tools like curl religiously print whatever results they get from the source and should never spontaneously print a newline without being asked to. I see this behaviour most often with curl. If you are coding a tool that uses curl, you do of course have the option of adding in a newline yourself.

I suggest not adding a newline unless you really have to. In the case where you really want to add a newline, you can use a separate tool (echo for example) - but the easiest with curl is the "write-out" option:

$ curl http://api.macvendors.com/0015c7   
Cisco Systems, Inc%     
$ curl -w '\n' http://api.macvendors.com/0015c7
Cisco Systems, Inc
$

From curl's man page:

   -w, --write-out <format>
          Make  curl  display  information  on stdout after a completed transfer. The format is a string that may contain plain text
          mixed with any number of variables. The format can be specified as a literal "string", or you can have curl read the  for-
          mat from a file with "@filename" and to tell curl to read the format from stdin you write "@-".

Try explicitly adding a newline \n to the message.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!