<> == How can I redirect the output of multiple commands at once? == Redirecting the standard output of a single command is as easy as: {{{#!highlight bash date > file }}} To redirect standard error: {{{#!highlight bash date 2> file }}} To redirect both: {{{#!highlight bash date > 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 }}} 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: {{{#!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" }}} (See [[BashFAQ/106|FAQ 106]] for more complex script logging techniques.) Otherwise, command grouping helps: {{{#!highlight bash { date # some other commands echo done } > messages.log 2>&1 }}} In this example, the output of all commands within the curly braces is redirected to the file {{{messages.log}}}. [[http://wiki.bash-hackers.org/syntax/redirection|More discussion]] [[http://wiki.bash-hackers.org/howto/redirection_tutorial|In-depth: Illustrated Tutorial]] ---- CategoryShell