2016-08-18 6 views
1

の違いは何である私は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" 

ので、エコーの最初のインスタンスでは、我々は、ログディレクトリ内のファイル.gitignoreに文字列を書いています。 2番目の例では、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 

は、ログディレクトリが作成され、ログ・ファイルはgitのためにコミットされないように、その後 *.loglog/.gitignoreに入れています。 >が使用されていたため、.gitignoreファイルが存在していた場合は、 *.logで上書きされます。

ログディレクトリ自体がローカルのgitステージに追加されます。

次の行には、tmpが上書きされるのではなく、.gitignoreファイルの末尾に追加されるように、>>が追加されています。ステージング領域に追加されます。

1

>はリダイレクト演算子です。 < > >| << >> <& >& <<- <>はすべてシェルコマンドインタープリタのリダイレクト演算子です。

例では、実質的に>が上書きされ、>>が上書きされます。

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. 
関連する問題