This only works because tr
does not change the file size.
1<>file
opens file
as standard output in overwrite mode. (<>
is called read/write mode, but since few programs read stdout
, it's more useful to focus on what it actually does.)
Normally, when you redirect output (>file
), the file is opened in "write" mode, which causes it to either be created or emptied. Another common option is >>file
, "append" mode, which skips the step where the file is emptied, but puts all output at the end. 1<>file
also skips emptying the file, but it puts the write cursor at the beginning of the file. (You need the 1
because <>
defaults to redirecting stdin
, not stdout
).
This is only very occasionally useful, since very few utilities are so precise in their modification. Another case would be a search and replace where the replacement is exactly the same length as the original. (A shorter replacement wouldn't work either, because the file is not truncated at the end; if the output is shorter than the original, you'd end up with whatever used to be at the end of the file still being at the end of the file.)