Select the TWO true statements about output redirection in bash.
LPIC-1 Exam 101-500, objective 103. GNU and Unix commands hard
Draft — not checked yet. This question was written from the published exam objectives and cites the clause it comes from, but nobody has yet read it against that clause and signed for it.
It is kept out of search results and out of mock exams until they have. Read the source below before you take the answer as settled.
The options
Choose 2.
Not correct > out.log adds to the end of out.log if the file already exists.
Wrong. > truncates an existing file to zero length before writing. Appending is >>.
Not correct A pipe passes both standard output and standard error to the next command.
Wrong. A plain | passes only standard output; standard error still goes to the terminal. Use |& in bash, or 2>&1 | , to pipe both.
Not correct In `cmd 2>&1 > out.log`, both streams end up in out.log.
Wrong, and this is the classic trap. Redirections are processed left to right: 2>&1 first points stderr at wherever stdout currently goes (the terminal), and only then is stdout moved to the file. The correct order is `cmd > out.log 2>&1`.
Correct 2> errors.log sends only standard error to the file, leaving standard output on the terminal.
Correct. 2 is the file descriptor for standard error, so only that stream is redirected.
Correct &> all.log sends both standard output and standard error to the file.
Correct in bash (and the equivalent portable form is > all.log 2>&1). Both streams end up in the same file, which is truncated first.
Why
Every process starts with three descriptors: 0 stdin, 1 stdout, 2 stderr. > and >> redirect descriptor 1 (truncating or appending), 2> and 2>> redirect descriptor 2, and n>&m makes descriptor n a duplicate of descriptor m at that moment. Because duplication copies the CURRENT target, the order of redirections on the line changes the result.
Where this comes from
- Cited
- LPI exam objective 103.4
- What it says
- Redirect standard input, standard output and standard error, including combining streams.
Practise this
Reading one question is not practice. The trainer will draw a set from objective 103 and space the ones you get wrong.