Home > bash > bash – redirection

bash – redirection

Problem:
I needed my script to record both stdout and stderr to a log file.

Solution:

This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout.

grep da * 1>&2

eg:

#!/bin/bash
cd /home/user
python print_to_stdout.py 1>&2>> logfile.log

Update – 2017-02-24
Above method of redirecting does not seem to work so well within scripts. Better to use

cmd >> file.txt 2>&1

Which translates to

Quote – It’s simple redirection, redirection statements are evaluated, as always, from left to right. >>file : Red. STDOUT to file (append mode) (short for 1>>file) 2>&1 : Red. STDERR to “where stdout goes” Note that the interpretion “redirect STDERR to STDOUT” is wrong. – End Quote. By TheBonsai

From -> http://stackoverflow.com/questions/876239/how-can-i-redirect-and-append-both-stdout-and-stderr-to-a-file-with-bash

Source:
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html

Other Resources:

http://www.cyberciti.biz/faq/redirecting-stderr-to-stdout/

Advertisements
Categories: bash Tags:
  1. December 29, 2012 at 12:42 pm

    there is a shorter way:

    ls file_that_does_not_exist &> stdout_and_stderr.txt

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: