Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What method should I use to write error messages to 'stderr' using 'printf' in a bash script?

I want to direct the output of a printf in a bash script to stderr instead of stdout.

I am not asking about redirecting either stderr or stdout from where ever they are currently routed. I just want to be able to send the output from a printf to stderr instead of to the default of stdout.

I experimented a little and found that appending 1>&2 to the printf, as shown in the example below, appears to do what I want. However, I have no experience using bash. So my primary question is if there is a "better" way to do this in bash?

By "better" I mean is there another way to do this which is more commonly used, more conventional, or more idiomatic? How would a more experienced bash programmer do it?

#!/bin/bash printf "{%s}   This should go to stderr.\n" "$(date)" 1>&2  printf "[(%s)] This should go to stdout.\n" "$(date)"  

I also have a secondary question. I am asking it not so much because I need to know, but more because I am just curious and would like to have a better understanding about what is happening.

It seems the above will only work when it runs inside a shell script. It does not appear to work when I try it from a command line.

Here is an example of what I mean.

irrational@VBx64:~$ printf "{%s} Sent to stderr.\n" "$(date)" 1>&2 2> errors.txt {Sat Jun  9 14:08:46 EDT 2012} Sent to stderr. irrational@VBx64:~$ ls -l errors.txt -rw-rw-r-- 1 irrational irrational 0 Jun  9 14:39 errors.txt 

I would expect the printf command above to have no output because the output should go to stderr, which in turn should go to a file. But this does not happen. Huh?

like image 692
irrational John Avatar asked Jun 09 '12 18:06

irrational John


People also ask

How do I redirect standard stderr error in bash?

Understanding the concept of redirections and file descriptors is very important when working on the command line. To redirect stderr and stdout , use the 2>&1 or &> constructs.

How do I print to stderr?

Use the fprintf Function to Print to stderr in C standard input ( stdin ) - used for reading input. standard output ( stdout ) - used for writing output. standard error stream ( stderr ) - used to log error or debug messages during run-time.


2 Answers

First, yes, 1>&2 is the right thing to do.

Second, the reason your 1>&2 2>errors.txt example doesn't work is because of the details of exactly what redirection does.

1>&2 means "make filehandle 1 point to wherever filehandle 2 does currently" — i.e. stuff that would have been written to stdout now goes to stderr. 2>errors.txt means "open a filehandle to errors.txt and make filehandle 2 point to it" — i.e. stuff that would have been written to stderr now goes into errors.txt. But filehandle 1 isn't affected at all, so stuff written to stdout still goes to stderr.

The correct thing to do is 2>errors.txt 1>&2, which will make writes to both stderr and stdout go to errors.txt, because the first operation will be "open errors.txt and make stderr point to it", and the second operation will be "make stdout point to where stderr is pointing now".

like image 107
hobbs Avatar answered Sep 21 '22 09:09

hobbs


  1. That seems reasonable to me. You don't need the 1, though - it will work as is, but it's implied:

    printf "{%s}   This should go to stderr.\n" "$(date)" >&2  
  2. You have an order-of-operations problem:

    $ printf "{%s} Sent to stderr.\n" "$(date)" 2> errors.txt 1>&2 $ ls -l errors.txt  -rw-r--r--  1 carl  staff  47 Jun  9 11:51 errors.txt $ cat errors.txt  {Sat Jun  9 11:51:48 PDT 2012} Sent to stderr. 
like image 37
Carl Norum Avatar answered Sep 19 '22 09:09

Carl Norum