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.

Since command substitution is evaluated earlier than the redirection, it can be used to avoid the problem. It's still a little awkward as good as creating temporary files though.

$ cat file
bravo
delta
charlie
alpha
charlie
$ echo "$(cat file | sort | uniq)" > file
$ cat file
alpha
bravo
charlie
delta
$

See Also

Gentaro "hibariya" Terada

Otaka-no-mori, Chiba, Japan
Email me

Loves Ruby, Internet, and Programming.