Menu

UNIX TUTORIALS - Unix - IO Redirections

Unix - IO Redirections

ADVERTISEMENTS

Redirection Commands:

CommandDescription
pgm > fileOutput of pgm is redirected to file
pgm < fileProgram pgm reads its input from file.
pgm >> fileOutput of pgm is appended to file.
n > fileOutput from stream with descriptor n redirected to file.
n >> fileOutput from stream with descriptor n appended to file.
n >& mMerge output from stream n with stream m.
n <& mMerge input from stream n with stream m.
<< tag Standard input comes from here through next tag at start of line.
|Takes output from one program, or process, and sends it to another.

ADVERTISEMENTS

Output Redirection:

$ who > users

ADVERTISEMENTS

$ cat users
oko         tty01   Sep 12 07:30
ai          tty15   Sep 12 13:32
ruth        tty21   Sep 12 10:10
pat         tty24   Sep 12 13:07
steve       tty25   Sep 12 13:03
$

$ echo line 1 > users
$ cat users
line 1
$

$ echo line 2 >> users
$ cat users
line 1
line 2
$

Input Redirection:

$ wc -l users
2 users
$

$ wc -l < users
2
$

Here Document:

command << delimiter
document
delimiter

$wc -l << EOF
	This is a simple lookup program 
	for good (and bad) restaurants
	in Cape Town.
EOF
3
$

#!/bin/sh

cat << EOF
This is a simple lookup program 
for good (and bad) restaurants
in Cape Town.
EOF	

This is a simple lookup program
for good (and bad) restaurants
in Cape Town.

#!/bin/sh

filename=test.txt
vi $filename <<EndOfCommands
i
This file was created automatically from
a shell script
^[
ZZ
EndOfCommands

$ sh test.sh
Vim: Warning: Input is not from a terminal
$

$ cat test.txt
This file was created automatically from
a shell script
$

Discard the output:

$ command > /dev/null

$ command > /dev/null 2>&1

$ echo message 1>&2