V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
nahcikuy
V2EX  ›  Linux

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

  •  
  •   nahcikuy · Jan 28, 2020 via Android · 7168 views
    This topic created in 2280 days ago, the information mentioned may be changed or developed.
    一个是参数复读姬
    一个是输入流复读姬
    23333
    12 replies    2020-01-30 18:19:26 +08:00
    hankai17
        1
    hankai17  
       Jan 28, 2020 via Android
    举个栗子
    nahcikuy
        2
    nahcikuy  
    OP
       Jan 28, 2020 via Android
    echo 2333 | echo 和
    echo 2333 | cat 的区别 QAQ
    oIMOo
        3
    oIMOo  
       Jan 28, 2020
    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
       Jan 28, 2020 via Android
    Linux 下的表现是相反的
    echo 会复读接收的参数但是对输入流的内容视而不见所以
    echo 23333 | echo
    不会输出任何东西。
    cat 在不提供参数的情况下会复读标准输入流的内容,所以
    echo 23333 | cat
    可以正常输出 23333。
    试着直接执行无参数的喵你就获得了一只无限复读你输入内容的复读姬 ww
    Buges
        5
    Buges  
       Jan 28, 2020 via Android   ❤️ 3
    @oIMOo 因为你调用的不是 cat,而是 PowerShell 函数 Get-Content 的 alias
    你调用的 echo 也是 Write-Output 的 alias
    输入 gal 或 Get-Alias 可以看到这些内置的 alias,微软为了用户体验接近*nix 风格也是煞费苦心,就是有点不伦不类。
    要调用实际的程序的话需要在名称后面加.exe
    minmini
        6
    minmini  
       Jan 28, 2020
    $ echo 2333 | cat
    2333
    $ echo 2333 | echo

    $
    mac 是这样的呢
    yexiaoxing
        7
    yexiaoxing  
       Jan 28, 2020
    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  
       Jan 28, 2020
    @Buges #5 感谢指导~~~
    Get-Alias 果然看到了背后的命令。
    工作电脑不给 admin 权限,极限就是用 Cross-platform PowerShell https://aka.ms/pscore6,然而对 linux 用户并没有有友好到哪里去……
    KentY
        9
    KentY  
       Jan 29, 2020
    @nahcikuy echo 23333 | echo 将会输出一个换行.
    aliipay
        10
    aliipay  
       Jan 29, 2020
    @oIMOo busybox 被裁减的惨不忍睹,能不碰就别碰
    oIMOo
        11
    oIMOo  
       Jan 29, 2020
    @aliipay #10 macOS + Linux 用户,结果工作电脑是没有 admin 的 Windows。
    虽然工作不用代码,但是偶尔自己会写点什么,无奈就装了……
    SpringHack
        12
    SpringHack  
       Jan 30, 2020
    试试 echo * 之前被坑过一次
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5657 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 08:29 · PVG 16:29 · LAX 01:29 · JFK 04:29
    ♥ Do have faith in what you're doing.