Distributions
› Ubuntu
› Fedora
› CentOS
中文资源站
› 网易开源镜像站
nahcikuy
V2EX  ›  Linux

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

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

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