UNIX TUTORIALS - Unix - IO Redirections
Unix - IO Redirections
ADVERTISEMENTS
Redirection Commands:
Command | Description |
---|---|
pgm > file | Output of pgm is redirected to file |
pgm < file | Program pgm reads its input from file. |
pgm >> file | Output of pgm is appended to file. |
n > file | Output from stream with descriptor n redirected to file. |
n >> file | Output from stream with descriptor n appended to file. |
n >& m | Merge output from stream n with stream m. |
n <& m | Merge 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 > usersADVERTISEMENTS
$ 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