redirect - In UNIX what "cat file1 > file1 does?" -


when output of command redirected file, output file created or truncated shell before command executed, idea cat foo > foo does?

in cases, file truncated. that’s because redirection handled shell, opens file writing before invoking command.

cat foo > foo 

the shell truncates , opens foo writing, sets stdout file, , exec’s ["cat", "foo"].

gnu cat smart , refuses redirect file itself. checking device/inode pair on input , output file descriptors; can read wonderful low-level details in src/cat.c. prints message , quits.

bsd cat doesn’t have such safety, since file has been truncated, there nothing read, nothing write, , halt.


you can spice things bit appending instead of truncating.

echo hello > foo cat foo >> foo 

now same except shell opens file appending instead of truncating it.

gnu cat sees you’re doing , stops; file untouched.

bsd cat goes loop , appends file until disk fills up.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -