When running the command

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

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

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.

题解

「sed」指令可以处理文件的文字内容,能够进行许多不同的动作组合,动作是使用一串特定格式的字符串来定义。「-e」参数的作用可以加入动作组合给「sed」指令,如果没有使用「-e」参数或是「-f」参数,就会以第一个参数作为动作组合。

在这题目中的指令,会读取「/tmp/file」文件,并将每行第一次出现的「a」取代成「b」,再将结果存回「/tmp/file」文件。但是「sed」指令在刚开始运行的时候,「/tmp/file」就会被先覆写成没有内容的文件,所以「sed」指令就不会从「/tmp/file」读取到任何的内容,因此答案是选项 C。