代码之家  ›  专栏  ›  技术社区  ›  abjoshi - Reinstate Monica

zsh中echo与printf命令行参数的不同行为

zsh
  •  0
  • abjoshi - Reinstate Monica  · 技术社区  · 4 年前

    我有一个简单的程序,可以用zsh打印命令行参数。然而,我看到printf和echo的不同行为。有人能解释一下吗?

    #!/bin/zsh
    echo "$# @ : "$@"   "
    printf "\n"
    printf "$# *  $*   "
    printf "\n"
    printf "$# @ : "$@" \n"
    

    输出:

    batman$ ./args  a b c d
    4 @ : a b c d   
    
    4 *  a b c d   
    4 @ : abatman$ //also gobbles up the newline!!
    batman$ 
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Marlon Richert    4 年前

    echo printf (及 print )是具有不同语法和行为的不同命令。阅读文档了解更多信息:

    在Zsh中,您应该始终选择 打印 printf 回响 . 回响 主要是为了与其他shell兼容。