Redirect

A redirect or redirection is the mechanism with that output is sent to a different destination than its default. You can redirect the output of commands or your own input to them.

Redirecting output
For instance, echo's standard output is directed to the console whereas the shell, with a redirection operator can send that to e.g. a file.

$ echo foo > bar

sends the string "foo" to bar rather than the screen. bar is an ordinary file, but can also be a device.

$ echo foo >> bar

will append the string "foo" to the ordinary file "bar".

$ echo foo > bar is short for $ echo foo 1> bar which means that the stream 1, stdout will be redirected to bar. You can redirect output from any command: $ ls 1> myfiles will create a file "myfiles" and store the output of ls there. Please note that not everything that you see is in the stdout stream. Take for example: duffman:~ # ls a /bin/ls: a: No such file or directory duffman:~ # ls a 1>/dev/null /bin/ls: a: No such file or directory duffman:~ # The redirection does not work here - obviously, the error message ("No such file or directory") has been sent to another stream than stdout. It has been sent to stderr (Standard Error Stream). The default stream numbers are as follows: So, the following redirects the error message to /dev/null: duffman:~ # ls a 2>/dev/null duffman:~ # In the following, there is an example of a command (ls text a) that outputs one line to stderr and one to stdout. You can find there how to unite both streams (stdin and stdout). duffman:~ # ls text a /bin/ls: a: No such file or directory text duffman:~ # ls text a 2>/dev/null text duffman:~ # ls text a 1>/dev/null /bin/ls: a: No such file or directory duffman:~ # ls text a 2>&1 | wc -l 2 duffman:~ # The redirection "2>&1" makes that the error messages are sent to stdout as well. You can count the number of lines produces with a pipe and wc. Now the following example sends all output from stdout and stderr to a file: duffman:~ # ls text a >text 2>&1 duffman:~ # cat text /bin/ls: a: No such file or directory text duffman:~ #
 * 0 for stdin
 * 1 for stdout
 * 2 for stderr

Redirecting input
The command cat > file << EOF will redirect the stdin-stream to the ordinary file "file". It will not stop sending the input there till the input is "EOF". So, the following example will create a file "text" with 3 lines: duffman:~ # cat >text< my first line > my second line > my last line before END > END duffman:~ #