简体   繁体   English

使用回声; > 和 >> 有什么区别

[英]Using echo; What is difference between > and >>

I came across this on a git site:我在 git 站点上遇到了这个:

mkdir log
echo '*.log' > log/.gitignore
git add log
echo tmp >> .gitignore
git add .gitignore
git commit -m "ignored log files and tmp dir"

So in the first instance of echo, we are writing the string to the file .gitignore in the log dir.因此,在 echo 的第一个实例中,我们将字符串写入日志目录中的 .gitignore 文件。 In the second instance, are we writing tmp to the file .gitignore (in current dir).在第二种情况下,我们是否将 tmp 写入文件 .gitignore(在当前目录中)。 Why do we need to use >> versus > ?为什么我们需要使用 >> 与 > ?

When echoing something to a file, >> appends to the file and > overwrites the file.将某些内容回显到文件时, >>附加到文件中,而>覆盖文件。

$ echo foobar > test
$ cat test
foobar
$ echo baz >> test
$ cat test
foobar
baz
$ echo foobar > test
$ cat test
foobar

From the example you posted, a log directory is created and then *.log is put into log/.gitignore so that no log files are committed to git.从您发布的示例中,创建了一个日志目录,然后将*.log放入log/.gitignore以便没有日志文件提交给 git。 Since > was used, if a .gitignore file had existed, it would be overwritten with only *.log .由于>被使用,如果一个 .gitignore 文件已经存在,它将被仅覆盖*.log

The log directory itself is then added to your local git stage.然后将日志目录本身添加到本地 git 阶段。

On the next line, >> is added so that tmp is appended to the end of the .gitignore file instead of overwriting it.在下一行,添加>>以便将tmp附加到 .gitignore 文件的末尾而不是覆盖它。 It is then added to the staging area.然后将其添加到暂存区。

> is a redirection operator. >是重定向操作符。 < > >| << >> <& >& <<- <> < > >| << >> <& >& <<- <> are all redirection operators in the shell command interpreter. < > >| << >> <& >& <<- <>都是 shell 命令解释器中的重定向操作符。

In your examples, essentially > overwrites and >> appends.在您的示例中,本质上是>覆盖和>>附加。

See man sh , (from your terminal you can access the manual via man sh ).请参阅man sh ,(从您的终端,您可以通过man sh访问手册)。

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM