Lets take an example :
i have an executable named a.out. This contains binary information to print some text to STDOUT cos of printf.
So when I give ./a.out, i see output of printf at the console STDOUT
Say if i do ‘./a.out > tempFile’ in console. How does this work? Since there is printf inside a.out, ideally i except the text to be printed in STDOUT. How does redirection consume this text and why do we not see any output in console and only in the file we see the printf text
In UNIX, everything is a file. All
stdoutis by default is the (for example)/dev/ttyfile which is a device driver hooked up to your console/terminal/window. Output is just sent to that file (device driver) which causes it to be output to whatever you’re using for interactive I/O.All the a command like
a.out >xyzzy.txtdoes is first connect the standard output of the program to that file rather than/dev/tty, hence the output shows up there instead.