V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
nahcikuy
V2EX  ›  Linux

今天突然发现 echo 和 cat 的区别原来是

  •  
  •   nahcikuy · 2020-01-28 22:25:08 +08:00 via Android · 6287 次点击
    这是一个创建于 1511 天前的主题,其中的信息可能已经有所发展或是发生改变。
    一个是参数复读姬
    一个是输入流复读姬
    23333
    12 条回复    2020-01-30 18:19:26 +08:00
    hankai17
        1
    hankai17  
       2020-01-28 22:34:34 +08:00 via Android
    举个栗子
    nahcikuy
        2
    nahcikuy  
    OP
       2020-01-28 22:36:02 +08:00 via Android
    echo 2333 | echo 和
    echo 2333 | cat 的区别 QAQ
    oIMOo
        3
    oIMOo  
       2020-01-28 22:49:13 +08:00
    Emmm
    Windows 装了 busybox 或者 coreutils,cat 和 echo 的效果确实不一样。
    ---
    > echo 2333 | echo
    2333
    > echo 2333 | cat
    cat : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
    At line:1 char:13
    + echo 2333 | cat
    + ~~~
    + CategoryInfo : InvalidArgument: (2333:PSObject) [Get-Content], ParameterBindingExcep tion
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.GetContentCommand
    nahcikuy
        4
    nahcikuy  
    OP
       2020-01-28 22:57:06 +08:00 via Android
    Linux 下的表现是相反的
    echo 会复读接收的参数但是对输入流的内容视而不见所以
    echo 23333 | echo
    不会输出任何东西。
    cat 在不提供参数的情况下会复读标准输入流的内容,所以
    echo 23333 | cat
    可以正常输出 23333。
    试着直接执行无参数的喵你就获得了一只无限复读你输入内容的复读姬 ww
    Buges
        5
    Buges  
       2020-01-28 22:58:00 +08:00 via Android   ❤️ 3
    @oIMOo 因为你调用的不是 cat,而是 PowerShell 函数 Get-Content 的 alias
    你调用的 echo 也是 Write-Output 的 alias
    输入 gal 或 Get-Alias 可以看到这些内置的 alias,微软为了用户体验接近*nix 风格也是煞费苦心,就是有点不伦不类。
    要调用实际的程序的话需要在名称后面加.exe
    minmini
        6
    minmini  
       2020-01-28 22:59:24 +08:00
    $ echo 2333 | cat
    2333
    $ echo 2333 | echo

    $
    mac 是这样的呢
    yexiaoxing
        7
    yexiaoxing  
       2020-01-28 23:14:39 +08:00
    The cat utility reads files sequentially, writing them to the standard output. The file operands are processed in command-line order. If file is a single dash (`-') or absent, cat reads from the standard input.

    The echo utility writes any specified operands, separated by single blank (` ') characters and followed by a newline (`\n') character, to the standard output.
    oIMOo
        8
    oIMOo  
       2020-01-28 23:23:24 +08:00
    @Buges #5 感谢指导~~~
    Get-Alias 果然看到了背后的命令。
    工作电脑不给 admin 权限,极限就是用 Cross-platform PowerShell https://aka.ms/pscore6,然而对 linux 用户并没有有友好到哪里去……
    KentY
        9
    KentY  
       2020-01-29 00:26:35 +08:00
    @nahcikuy echo 23333 | echo 将会输出一个换行.
    aliipay
        10
    aliipay  
       2020-01-29 15:16:34 +08:00
    @oIMOo busybox 被裁减的惨不忍睹,能不碰就别碰
    oIMOo
        11
    oIMOo  
       2020-01-29 16:36:53 +08:00
    @aliipay #10 macOS + Linux 用户,结果工作电脑是没有 admin 的 Windows。
    虽然工作不用代码,但是偶尔自己会写点什么,无奈就装了……
    SpringHack
        12
    SpringHack  
       2020-01-30 18:19:26 +08:00
    试试 echo * 之前被坑过一次
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5300 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:37 · PVG 17:37 · LAX 02:37 · JFK 05:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.