Redirection operator

From LQWiki
Jump to navigation Jump to search

A redirection operator redirects the output of a command. This article only talks about redirection operators in the bash.

Example:

echo foo > bar

here, ">" is the redirection operator, "echo" is the command, "foo" is the output that is redirected to the file "bar". > redirects stdout from the command left of it to the file named right of the greater-sign. The file is rewritten. If you do not want to redirect stdout, replace > with 2> for stderr. If you want the content to be appended to the file, replace > with >>.

These operators can be combined, but you have to take care for the right order, e.g.:

ls this_file_does_not_exist >>result 2>&1

A bit different is piping that is not really a redirect. A pipe "redirects" the output of the command left of it to the standard input of its right program, for example:

echo "this is your mail"|sendmail me@home.de

This sends a string to the program sendmail that sends it as a mail to you at home.