The unpredictability happens because there's a race condition between two stages in the pipeline, cat "$file"
and cat -s > "$file"
.
The first tries to open the file and read from it, while the other tries to empty the file.
- If it's emptied before it's read, you get an empty file.
- If it's read before it's emptied, you get some data (but the file is emptied shortly after and the result is truncated unless it's very short).
If you have GNU sed, you can simply do sed -i 'expression' *