I am trying to sort a file with following command : sort ${filename} > ${filename}
, and I get as result - an empty file.
Can someone explain why I get this result and how can I fix it?
Asked
Active
Viewed 187 times
-2

Software_t
- 576
- 1
- 4
- 13
2 Answers
1
What you are trying to do is a inplace sort
so which may not be supported, try instead.
sort ${filename} > temp_file && mv temp_file ${filename}

RavinderSingh13
- 130,504
- 14
- 57
- 93
1
In short, because Bash truncates the file before sort
can read from it. Apart from @RavinderSingh13’s answer, which is the standard way to fix it, you could also use sponge
from the moreutils
package:
sort ${filename} | sponge ${filename}

Boldewyn
- 81,211
- 44
- 156
- 212