Differences between revisions 5 and 11 (spanning 6 versions)
Revision 5 as of 2009-05-27 01:53:52
Size: 1262
Editor: localhost
Comment: [igli] oops; links were round wrong way
Revision 11 as of 2015-03-05 00:29:41
Size: 1502
Editor: izabera
Comment: syntax hl
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
Redirecting the standard output of a single command is as easy as Redirecting the standard output of a single command is as easy as:
Line 5: Line 5:
{{{
    date > file
{{{#!highlight bash
date > file
Line 10: Line 10:
{{{
    date 2> file
{{{#!highlight bash
date 2> file
Line 15: Line 15:
{{{
    date > file 2>&1
{{{#!highlight bash
date > file 2>&1
Line 19: Line 19:
In a loop or other larger code structure:
{{{
    for i in $list; do
        echo "Now processing $i"
        # more stuff here...
    done > file 2>&1
or, a fancier way:
{{{#!highlight bash
# Bash only. Equivalent to date > file 2>&1 but non-portable.
date &> file
}}}

Redirecting an entire loop:
{{{#!highlight bash
for i in "${list[@]}"; do
    echo "Now processing $i"
    # more stuff here...
done > file 2>&1
Line 29: Line 35:
{{{
    # redirect both standard output and standard error to "log.txt"
    exec > log.txt 2>&1
    # all output including stderr now goes into "log.txt"
{{{#!highlight bash
# redirect both standard output and standard error to "log.txt"
exec > log.txt 2>&1
# all output including stderr now goes into "log.txt"
Line 35: Line 41:
Otherwise command grouping helps: (See [[BashFAQ/106|FAQ 106]] for more complex script logging techniques.)
Line 37: Line 43:
{{{
    {
     date
     # some other command
     echo done
    } > messages.log 2>&1
Otherwise, command grouping helps:

{{{#!highlight bash
{
    date
    # some other commands
    echo done
} > messages.log 2>&1
Line 47: Line 55:
[[http://bash-hackers.org/wiki/doku.php/syntax/redirection|More discussion]] [[http://wiki.bash-hackers.org/syntax/redirection|More discussion]]
Line 49: Line 57:
[[http://bash-hackers.org/wiki/doku.php/howto/redirection_tutorial|In-depth: Illustrated Tutorial]] [[http://wiki.bash-hackers.org/howto/redirection_tutorial|In-depth: Illustrated Tutorial]]

----
CategoryShell

How can I redirect the output of multiple commands at once?

Redirecting the standard output of a single command is as easy as:

   1 date > file

To redirect standard error:

   1 date 2> file

To redirect both:

   1 date > file 2>&1

or, a fancier way:

   1 # Bash only.  Equivalent to date > file 2>&1 but non-portable.
   2 date &> file

Redirecting an entire loop:

   1 for i in "${list[@]}"; do
   2     echo "Now processing $i"
   3     # more stuff here...
   4 done > file 2>&1

However, this can become tedious if the output of many programs should be redirected. If all output of a script should go into a file (e.g. a log file), the exec command can be used:

   1 # redirect both standard output and standard error to "log.txt"
   2 exec > log.txt 2>&1
   3 # all output including stderr now goes into "log.txt"

(See FAQ 106 for more complex script logging techniques.)

Otherwise, command grouping helps:

   1 {
   2     date
   3     # some other commands
   4     echo done
   5 } > messages.log 2>&1

In this example, the output of all commands within the curly braces is redirected to the file messages.log.

More discussion

In-depth: Illustrated Tutorial


CategoryShell

BashFAQ/014 (last edited 2015-03-05 00:29:41 by izabera)