Alexander Arendar Alexander Arendar - 3 months ago 9
Bash Question

Unclear moment in output redirect

I have two files

text1
and
text2
in the
~/Documents
folder.
When I enter the command

ls >listing
and then view the content of the
listing
file I can see

listing
text1
text2


Could you explain technically why
listing
is included? I thought that it takes the output of the
ls
command which would have only 2 lines, then creates the
listing
file and puts those 2 lines there. But apparently not.

Answer

The shell creates 'listing' as soon as it parses your command line; it has to call open(2) to obtain a file descriptor to pass to the forked process for ls. Thus it's there by the time ls starts.

Comments