Read a File and Redirect the Output to the Same File
To write outputs to a file in a shell, we can use redirection. However, when we read a file and modify the content and redirect to the same original file, we’ll get just an empty file. What’s going on here?
$ cat file bravo delta charlie alpha charlie $ cat file | sort | uniq > file $ cat file # empty file $ Of course, if you redirect to a different file and rename it to the original name, it works as expected. This behavior is a little annoying when we want to modify a file like sorting and removing duplicates and do it without creating any temporary file. The reason redirecting output to the source file ends up an empty file is the redirection truncates the destination file earlier than the command actually starts processing.