【问题标题】:What is the difference beetwen $ echo Hello world > file and $ echo Hello > file world in bash?bash 中 $ echo Hello world > file 和 $ echo Hello > file word 有什么区别?
【发布时间】:2015-10-22 21:49:40
【问题描述】:
$ echo Hello world > file
$ echo Hello > file world
$ echo > file Hello world
$ > file echo Hello world

他们都做同样的事情,但我不知道为什么。

【问题讨论】:

  • 重定向 (>) 在技术上可以发生在命令行的任何位置。虽然第一个选项是“传统”,但其他选项都做同样的事情,因为它们重定向到文件 (> file),然后执行命令行左侧的任何内容。
  • 我希望 Marc 的评论是一个答案,这样我就可以投票了
  • 请修正标题,因为两个版本是一样的!

标签: bash shell console command


【解决方案1】:

来自man bash

   Simple Commands
       A  simple  command  is a sequence of optional variable assignments fol-
       lowed by blank-separated words and redirections, and  terminated  by  a
       control operator.  The first word specifies the command to be executed,
       and is passed as argument zero.  The  remaining  words  are  passed  as
       arguments to the invoked command.

也就是说,它没有指定“单词和重定向”的顺序。 稍后在重定向部分:

REDIRECTION
       [...]
       The [...] redirection  opera-
       tors may precede or appear anywhere within a simple command or may fol-
       low a command.  Redirections are processed in the  order  they  appear,
       from left to right.
       [...]

所以它们可以出现在任何地方。

正如您自己也观察到的那样,就结果而言,它们之间没有区别。 但是在可读性上有所不同。

这是最直观的写法:

echo Hello world > file

真的很容易理解。 > 看起来像一个箭头,不是吗。

其他的不是那么直观,可读性较差。 我建议坚持第一种写作风格。

【讨论】:

    猜你喜欢
    • 2015-07-22
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 2020-04-25
    • 2019-01-16
    • 2018-01-02
    相关资源
    最近更新 更多