Using echo; What is difference between > and >>
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. Since >
was used, if a .gitignore file had existed, it would be overwritten with only *.log
.
The log directory itself is then added to your local git stage.
On the next line, >>
is added so that tmp
is appended to the end of the .gitignore file instead of overwriting it. It is then added to the staging area.
>
is a redirection operator. < > >| << >> <& >& <<- <>
are all redirection operators in the shell command interpreter.
In your examples, essentially >
overwrites and >>
appends.
See man sh
, (from your terminal you can access the manual via 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.