What does >> or double Angle brackets mean? [duplicate]
Solution 1:
>>
can be used to pipe output into a text file and will append to any existing text in that file.
'any command' >>
textfile.txt
appends the output of 'any command' to the text file.
using >
will overwrite.
Solution 2:
The right angle bracket symbol (>) is used to redirect output to a disk file. If the file specified does not already exist, it is created; if it does exist, it is overwritten. The left angle bracket symbol (<) is used to redirect input from a disk file. To append output to an existing file, use double right angle brackets (>>)
Solution 3:
The >
and >>
are redirection operators for FD's (File Descriptors)
In bash you have tree standard FD's that are the standard input (strin), the standard output (strout) and the standard error (strerr). These can also be called by FD 0, FD 1 and FD 2 respectively.
Normally you would have you would have all FD's pointing to the terminal, but this can be changed by using redirection.
For example, if you call:
command > log.txt
You will redirect the output to the file log.txt This is similar as calling:
command 1> log.txt
As this only redirects strout you will still be able to see the error in the terminal. In order to redirect strerr to you log.txt file you will have to run:
command 2> log.txt
Again, this only redirects strerr. If you wish to redirect both stdout and stderr you need to duplicate your stderr output to stdout by using the >&
command.
command 1> log.txt 2>&1
To understand this command you need to read it form right to left, first a copy of stderr is made to stdout, then strout is redirected to the log.txt file.
When you use redirection in this way, bash will not look if the file exists or not and simply create one regardless if that implies erasing the existing one. If you want to avoid loosing what has already been written in your log file you can then use the >>
command in the same ways explained above, but in this case all the outputs will be appended to existing files.
For their use in C++ with cin, cout and cerr I think hash gave a better answer than I could.
I'm not an expert in these so I might have gotten some things wrong. For a more complete information I advise reading Bash guide on Greg's Wiki
Solution 4:
In C/C++:
In C/C++ the Left and Right Shift operators use the symbols <<
and >>
as the bitwise operator; which perform shift operations on bits. C++ also makes the use of overloaded bitwise shift operators in basic Input/Output operations; >>
and <<
brackets in C++ are used for extraction and insertion of data/information to streams which may be standard input/output, files.
In Shell Scripting/Programming:
In Shell Scripting/Programming, not very different from the extraction/insertion operations as mentioned above, the >>
/ <<
(variants of >
/ <
operators) are used to redirect standard streams from / to those defined by users and perform appending (differing from >
/ <
which overwrites) operations.
References:
-
Bitwise operations in C
-
Redirection in Computing
You might be interested in reading:
- Why are bitwise shifts (
<<
and>>
) used for cout and cin?