Program output redirection

fifofile-descriptorsio-redirection

When trying to redirect program output with the "some number greater than" syntax (eg foo 2> myfile), what are the possible numbers here and what do they represent?

I believe 1 is /dev/stdout, 2 is /dev/stderr. What about 5 & 6? Is there 3, 4 or some number greater than 6?

Best Answer

This supposed program will write to file descriptor number you specified. consider the following hello world program:

#include <stdio.h>

main()
{
   ssize_t i = 0 ;
   printf ("hello world\n") ;
   i = write( 5 , "Bonjour Monde\n", 14 ) ;
   printf ("%d octet dans 5\n", (int) i) ;
}

compile it

me@mybox:~/tmp7$ make hw
cc     hw.c   -o hw

now a simple run

me@mybox:~/tmp7$ ./hw
hello world
-1 octet dans 5

no file for 5, so no byte wrote.

next try:

me@mybox:~/tmp7$ ./hw 5> u
hello world
14 octet dans 5
me@mybox:~/tmp7$ cat u
Bonjour Monde

I manage to get an output while specifying a file and a file descriptor (e.g. 5>u ).

In practice, unless you have wrote such funny program as above, you are unlikely to collect data using 5>foo.

in shell script, construct using <( ) are more usefull:

 diff <( cmd -par 1 ) <(cmd -par 2)
Related Question