LPIC-1 Exam 101 Practice Questions

Linux Professional Institute LPIC-1 Exam 101 (101-500) exam preparation

LPIC-1 Exam 101 practice questions in English: 180 questions with explanations. The mock exam has 60 questions and 90 minutes.

LPIC-1 Exam 101 free question 6 / 10 · Domain 3: GNU and Unix Commands

/etc/hostname exists and /nope does not. You ran ls /etc/hostname /nope 2>&1 > out.txt. Which combination of what appears on the screen and what goes into out.txt is correct?

  1. Screen: no lines / out.txt: one line, /etc/hostname
  2. Screen: one error line / out.txt: two lines, the error and /etc/hostname
  3. Screen: no lines / out.txt: two lines, the error and /etc/hostname
  4. Screen: one error line / out.txt: one line, /etc/hostname
Show the answer and explanation

Answer: D

Why this is correct
Redirections are processed from left to right. The 2>&1 written first copies the current destination of standard output, the screen, as the destination of standard error. The next > out.txt redirects only standard output to out.txt. Standard error stays at the screen it was copied from, so the error that /nope does not exist appears on the screen, and out.txt receives only the one line /etc/hostname. bash(1) also says that in this order only standard output goes to the file.

Why the other options are wrong
"Screen: no lines / out.txt: two lines, the error and /etc/hostname" … That is the result of writing ls /etc/hostname /nope > out.txt 2>&1. If you first point standard output to the file and then duplicate it, the error goes into the same file.
"Screen: no lines / out.txt: one line, /etc/hostname" … An error vanishes from the screen when it is thrown away with 2>/dev/null. 2>&1 duplicates a destination and does not discard the error.
"Screen: one error line / out.txt: two lines, the error and /etc/hostname" … The same content appears on both the screen and the file when you put tee in between. Each output has one destination, and the error appears only on the screen or only in out.txt.

Good to remember
2>&1 means 'send standard error to the same place standard output goes now', and it does not follow if standard output is redirected afterward. To put both into one file, write ls ... > out.txt 2>&1 or ls ... &> out.txt.

Go to the LPIC-1 Exam 101 practice app (10 free questions; purchase unlocks all questions)

See the full practice set