bash - Why does `sort file > file` result in an empty file? -
this question has answer here:
- bash redirect input file same file 10 answers
when try sort file in-place with
sort afile > afile you silently end afile being empty file.
why that? i'd expect either error or original contents, sorted. haven't tested other shells.
upvotes one-liners perform expected behaviour.
ps bash redirect input file same file doesn't address "why" @ all. know can go around temporary file. interested in happens. upvotes one-liners part afterthought in search of shorter ways.
with sort afile > afile happens:
the shell opens , truncates
afilebecause of file direction operation> afilethe shell executes
sortprogram 1 argument,afile, , binds stdout of new process file descriptor opened in step 1.the sort program opens file given first argument, empty due truncation happening in step 1.
you can sort afile -o afile
Comments
Post a Comment