101-400 Exam QuestionsBrowse all questions from this exam

101-400 Exam - Question 79


When running the command -

sed -e "s/a/b/" /tmp/file >/tmp/file

While /tmp/file contains data, why is /tmp/file empty afterwards?

Show Answer
Correct Answer: C

When the shell establishes the redirection, it overwrites the target file before the redirected command starts and opens it for reading. In this case, the shell processes the `>/tmp/file` redirection before the `sed` command can read the original `/tmp/file` content. As a result, the file is emptied before `sed` can process it, leaving `/tmp/file` empty.

Discussion

1 comment
Sign in to comment
DuboisNicolasDuclairOption: C
Oct 21, 2023

The reason `/tmp/file` is empty after running the command: C. When the shell establishes the redirection, it overwrites the target file before the redirected command starts and opens it for reading. In this scenario, the shell processes the redirection before the `sed` command is executed. As a result, the output file, `/tmp/file`, is emptied before `sed` even begins processing the data from the input file. This is why the file ends up empty.