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

前端 未结 4 1729
抹茶落季
抹茶落季 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:14

    stdout and stderr will first prepared and then stdin and then the command execute. so a.txt would be clear for stdout first and then when the comamnd execute no content could be found. try sed -i 's/abd/def/g' a.txt or sed 's/abd/def/g' a.txt 1<> a.txt

提交回复
热议问题