LPIC-1 Exam 101 (Linux Administrator) — Question 49
When running the command -
sed -e "s/a/b/" /tmp/file >/tmp/file
While /tmp/file contains data, why is /tmp/file empty afterwards?
Answer options
- A. The file order is incorrect. The destination file must be mentioned before the command to ensure redirection.
- B. The command sed did not match anything in that file therefore the output is empty.
- C. When the shell establishes the redirection it overwrites the target file before the redirected command starts and opens it for reading.
- D. Redirection for shell commands do not work using the > character. It only works using the | character instead.
Correct answer: C
Explanation
The correct answer is C because when the shell processes the command, it creates the output file first, which clears its contents before sed has a chance to read it. Options A and D are incorrect as they misinterpret how shell redirection works, while B is wrong because the sed command is designed to make substitutions if matches exist.