gnu – Bloated echo命令




在他们的文章“UNIX环境中的程序设计”中,Pike& Kernighan讨论了cat程序如何增加控制参数.在某个地方,虽然不是那篇文章,但有一条关于“猫从伯克利挥舞旗帜回来”的评论.这与echo开发选项的问题类似. (我在BSD(Mac OS X)手册中找到了对相关文章的参考:Rob Pike,“UNIX Style,或者cat -v Considered Harmful”,USENIX Summer Conference Proceedings,1983.另见

在他们的书“The UNIX Programming Environment”中,Kernighan&派克(是的,那两个)再次引用道格麦克罗伊关于“回声”应该做什么没有争论的主题(大约1984年):

Another question of philosophy is what echo should do if given no arguments – specifically, should it print a blank line or nothing at all. All the current echo implementations we know print a blank line, but past versions didn’t, and there were great debates on the subject. Doug McIlroy imparted the right feelings of mysticism in his discussion on the topic:


There dwelt in the land of New Jersey the UNIX, a fair maid whom savants travelled far to admire. Dazzled by her purity, all sought to espouse her, one for her virginal grace, another for her polished civility, yet another for her agility in performing exacting tasks seldom accomplished even in much richer lands. So large of heart and accommodating of nature was she that the UNIX adopted all but the most insufferably rich of her suitors. Soon, many offspring grew and prospered and spread to the ends of the earth.

Nature herself smiled and answered to the UNIX more eagerly than to other mortal beings. Humbler folk, who knew little of more courtly manners, delighted in her echo, so precise and crystal clear they scarce believed she could be answered by the same rocks and woods that so garbled their own shouts into the wilderness. And the compliant UNIX obliged with perfect echoes of whatever she was asked.

When one impatient swain asked the UNIX, ‘Echo nothing’, the UNIX obligingly opened her mouth, echoed nothing, and closed it again.

‘Whatever do you mean,’ the youth demanded, ‘opening your mouth like that? Henceforth never open your mouth when you are supposed to echo nothing!’ And the UNIX obliged.

‘But I want a perfect performance, even when you echo nothing,’ pleaded a sensitive youth, ‘and no perfect echoes can come from a closed mouth.’ Not wishing to offend either one, the UNIX agreed to say different nothings for the impatient youth and the insensitive youth. She called the sensitive nothing ‘\n‘.

Yet now when she said ‘\n‘, she was not really saying nothing so she had to open her mouth twice, once to say ‘\n‘ and once to say nothing, and so she did not please the sensitive youth, who said forthwith, ‘The \n sounds like a perfect nothing to me, but the second one ruins it. I want you to take back one of them.’ So the UNIX, who could not abide offending, agreed to undo some echoes, and called that ‘\c‘. Now the sensitive youth could hear a perfect echo of nothing by asking for ‘\n‘ and ‘\c‘ together. But they say that he died of a surfeit of notation before he ever heard one.

Korn shell引入(或至少包括)一个基于C语言printf()函数的printf命令,它使用格式字符串来控制材质的显示方式.它是比echo更复杂的格式化工具.但是,由于报价中概述的历史,回声不仅仅是回声;它解释了回应的内容.


#include <stdio.h>
int main(int argc, char **argv)
    const char *pad = "";
    while (*++argv)
        fputs(pad, stdout);
        fputs(*argv, stdout);
        pad = " ";
    fputc('\n', stdout);
    return 0;

还有其他方法可以实现这一目标.但更复杂的echo版本必须在打印任何内容之前仔细检查它们的参数 – 这需要更多的代码.并且不同的系统已经决定他们想要对他们的参数进行不同数量的解释,从而导致不同数量的代码.


转载注明原文:gnu – Bloated echo命令