Saturday, 9 June 2012

Understanding the I/O redirection in linux

STDIN is FD 0, while STDOUT is FD 1, and STDERR is FD 2.


To redirect the errors to the output stream use this: 2>&1
Here '&' tells that it is a FD(file descriptor) and not an ordinary file named '1'.

There are lots of redirection symbols that you can use, and here are some of them:
filemeans open a file for reading and associate with STDIN.
<< tokenMeans use the current input stream as STDIN for the program until token is seen. We will ignore this one until we get to scripting.
filemeans open a file for writing and truncate it and associate it with STDOUT.
>> filemeans open a file for writing and seek to the end and associate it with STDOUT. This is how you append to a file using a redirect.
n>&mmeans redirect FD n to the same places as FD m. Eg, 2>&1 means send STDERR to the same place that STDOUT is going to.



No comments:

Post a Comment