Saturday, July 16, 2016

How to redirect file descriptions STDIN, STDOUT, and STDERR

there are three standard file descriptions, STDIN, STDOUT, and STDERR. They are assigned to 0, 1, and 2 respectively.

a)
STDIN (0):
$git --version 0>test.txt
$cat test.txt
$gitblabla --version 0>test.txt
$cat test.txt

Example:
$git --version 0>test.txt
git version 1.9.1 === Output printed in stdout
$cat test.txt
$gitblabla --version 0>test.txt
gitblabla: command not found === Error printed in stdout
$cat test.txt
$

b)
STDOUT (1):
$git --version 1>test.txt
$cat test.txt
$gitblabla --version 1>test.txt
$cat test.txt

Example:
$git --version 1>test.txt
$cat test.txt
git version 1.9.1 === Output redirected to file
$gitblabla --version 1>test.txt
gitblabla: command not found === Error printed in stdout
$cat test.txt
$

c)
STDERR (2):
$git --version 2>test.txt
$cat test.txt
$gitblabla --version 2>test.txt
$cat test.txt

Example:
$git --version 2>test.txt
git version 1.9.1 === Output printed in stdout
$cat test.txt
$gitblabla --version 2>test.txt
$cat test.txt
gitblabla: command not found === Error redirected to file


1 comment: