Why reading and writing the same file through I/O redirection results in an empty file in Unix?

前端 未结 4 1727
抹茶落季
抹茶落季 2020-12-11 04:16

If I redirect output of a command to same file it reads from, its contents is erased.

sed \'s/abd/def/g\' a.txt > a.txt

Can anyone expla

4条回答
  •  [愿得一人]
    2020-12-11 05:11

    The first thing the redirection does is to open the file for writing, thus clearing any existing contents. sed then tries to read this empty file you have just created, and does nothing. The file is then closed, containing nothing.

提交回复
热议问题