Reputation: 4166
If I do ls -la, I get results like
total 16
drwxr-xr-x 4 rockse staff 136 Apr 28 16:55 .
drwx------+ 23 rockse staff 782 Apr 28 16:48 ..
-rw-r--r-- 1 rockse staff 32 Apr 28 16:49 1.sh
-rw-r--r-- 1 rockse staff 215 Apr 28 17:01 ls-1.txt
But if I do ls -la > ls-1.txt, I get this
total 8
drwxr-xr-x 4 rockse staff 136 Apr 28 16:55 .
drwx------+ 23 rockse staff 782 Apr 28 16:48 ..
-rw-r--r-- 1 rockse staff 32 Apr 28 16:49 1.sh
-rw-r--r-- 1 rockse staff 0 Apr 28 17:06 ls-1.txt
I understand that a file is created and then ls -la
is written to the same but why doesn't it capture the snapshot of ls -la
before creating the file because we are just writing stdout to a file ?
Upvotes: 0
Views: 784
Reputation: 781726
The redirection is done by the shell, not the program you're running. The processing done by the shell to implement this is similar to this (simplified):
stdout
to the output file streamStep 2 creates the file, so it will be visible when the program runs in step 4.
If step 2 were done after step 4, it wouldn't be possible to change the program's stdout
to point to it.
Upvotes: 1