首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用echo;>和>>有什么区别

使用echo;>和>>有什么区别
EN

Stack Overflow用户
提问于 2016-08-19 01:21:04
回答 2查看 14.7K关注 0票数 12

我在一个git网站上发现了这个:

代码语言:javascript
运行
复制
mkdir log
echo '*.log' > log/.gitignore
git add log
echo tmp >> .gitignore
git add .gitignore
git commit -m "ignored log files and tmp dir"

因此,在echo的第一个实例中,我们将字符串写入日志目录中的文件.gitignore。在第二个实例中,我们是否将临时文件写入文件.gitignore (在当前目录中)。为什么我们需要使用>>而不是>?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-19 02:09:10

在向文件回显某些内容时,>>会附加到文件,而>会覆盖该文件。

代码语言:javascript
运行
复制
$ echo foobar > test
$ cat test
foobar
$ echo baz >> test
$ cat test
foobar
baz
$ echo foobar > test
$ cat test
foobar

在您发布的示例中,创建了一个日志目录,然后将*.log放入log/.gitignore,这样就不会将日志文件提交到git。由于使用了>,因此如果.gitignore文件已经存在,则只会用*.log覆盖该文件。

然后将日志目录本身添加到本地git工作台。

在下一行中,添加了>>,以便将tmp附加到.gitignore文件的末尾,而不是覆盖它。然后将其添加到临时区域。

票数 19
EN

Stack Overflow用户

发布于 2016-08-19 02:29:30

>是一个重定向操作符。< > >| << >> <& >& <<- <>都是shell命令解释器中的重定向操作符。

在您的示例中,本质上是>覆盖和>>附加。

请参阅man sh (从您的终端,您可以通过man sh访问手册)。

代码语言:javascript
运行
复制
Redirections
     Redirections are used to change where a command reads its input or sends its output.  In
     general, redirections open, close, or duplicate an existing reference to a file.  The over‐
     all format used for redirection is:

           [n] redir-op file

     where redir-op is one of the redirection operators mentioned previously.  Following is a
     list of the possible redirections.  The [n] is an optional number, as in '3' (not '[3]'),
     that refers to a file descriptor.

           [n]> file   Redirect standard output (or n) to file.

           [n]>| file  Same, but override the -C option.

           [n]>> file  Append standard output (or n) to file.

           [n]< file   Redirect standard input (or n) from file.

           [n1]<&n2    Duplicate standard input (or n1) from file descriptor n2.

           [n]<&-      Close standard input (or n).

           [n1]>&n2    Duplicate standard output (or n1) to n2.

           [n]>&-      Close standard output (or n).

           [n]<> file  Open file for reading and writing on standard input (or n).

     The following redirection is often called a "here-document".

           [n]<< delimiter
                 here-doc-text ...
           delimiter

     All the text on successive lines up to the delimiter is saved away and made available to the
     command on standard input, or file descriptor n if it is specified.  If the delimiter as
     specified on the initial line is quoted, then the here-doc-text is treated literally, other‐
     wise the text is subjected to parameter expansion, command substitution, and arithmetic
     expansion (as described in the section on "Expansions").  If the operator is "<<-" instead
     of "<<", then leading tabs in the here-doc-text are stripped.
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39024073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档