2016-08-18 23 views
1

我碰到這個來的Git網站的區別:使用echo;之間有什麼>和>>

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

所以回聲的第一個實例,我們正在寫字符串到日誌文件目錄文件的.gitignore。在第二種情況下,我們是否將tmp寫入文件.gitignore(在當前目錄中)。爲什麼我們需要使用>>與>?

+0

[之間>與差異>>在創建日誌文件]的可能的複製(http://stackoverflow.com/questions/34649227/difference-between-and-while-creating-log-file) –

回答

2

當對文件回顯某個內容時,>>附加到該文件並且>覆蓋該文件。

➜ ~ echo foobar > test 
➜ ~ cat test 
foobar 
➜ ~ echo baz >> test 
➜ ~ cat test 
foobar 
baz 
➜ ~ echo foobar > test 
➜ ~ cat test 
foobar 

從您發佈的示例中,創建一個日誌目錄,然後*.log使沒有日誌文件都致力於Git是投入log/.gitignore。由於使用了>,所以如果一個.gitignore文件已經存在,它將被覆蓋,只有*.log

日誌目錄本身,然後添加到您的本地git的階段。

在下一行添加>>,以便將tmp追加到.gitignore文件的末尾,而不是覆蓋它。然後將其添加到暫存區域。

1

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

在你的例子,基本上>覆寫和>>追加。

請參閱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. 
相關問題