bash - Why does `sort file > file` result in an empty file? -


this question has answer here:

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:

  1. the shell opens , truncates afile because of file direction operation > afile

  2. the shell executes sort program 1 argument, afile, , binds stdout of new process file descriptor opened in step 1.

  3. the sort program opens file given first argument, empty due truncation happening in step 1.

you can sort afile -o afile


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -