How to print to STDERR?

Common print functions print messages to the STDOUT. But how to print to STDERR?

asked Apr 1 by zma (2,200 points)

8 Answers

In Bash:

1>&2 echo "msg to STDERR"

The 1>&2 redirects STDOUT to STDERR.

answered Apr 1 by zma (2,200 points)

In Python:

import sys
print >> sys.stderr, "your message here"

An example:

$ python 2>/tmp/stderr
>>> import sys
>>> print >> sys.stderr, "your message here"
>>> exit()
$ cat /tmp/stderr 
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
your message here
answered Apr 2 by zma (2,200 points)

In Java, print a string str to STDERR as a new line:

System.err.println(str)

Standard error cab be accessed with 'System.err' in Java.

answered Apr 9 by zma (2,200 points)

In Go, os.Stderr is an io.Writer, so you can use it with any function that accepts an io.Writer. 2 of the common ways you may use:

import "os"

os.Stderr.WriteString("your message here")

or, throught fmt.Fprintf:

import "os"
import "fmt"

fmt.Fprintf(os.Stderr, "your message here");
answered Apr 9 by zma (2,200 points)

In C, you can print to the file handle stderr which is the STDERR:

#include <stdio.h>

fprintf(stderr, "fmt", ...);

One example:

stderr.c:

#include <stdio.h>

int main()
{
  fprintf(stderr, "hello world!\n");
}

Built and run it:

$ gcc stderr.c -o s && ./s
hello world!
answered Apr 12 by zma (2,200 points)

In C++, std::cerr is a stream to the STDERR.

You can use the common I/O operators like << or std::cerr to print content to the STDERR.

One example is in stderr.cc:

#include <iostream>

int main()
{
  std::cerr << "hello world!\n";
}

Built and run it:

$ g++ stderr.cc -o s && ./s
hello world!
answered Apr 12 by zma (2,200 points)
edited Apr 12 by zma

In Perl, print to STDERR which is the STDERR:

print STDERR "your message here"

For example,

$ perl -de1

Loading DB routines from perl5db.pl version 1.37
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

main::(-e:1):	1
  DB<1> print STDERR "hello world!"
hello world!
  DB<2>
answered Apr 12 by zma (2,200 points)

In OCaml, you can use the Printf.eprintf function to print to STDERR (which is in Pervasives.stderr).

Examples:

Use Printf.eprintf:

$ rlwrap ocaml 2>/tmp/a.txt
        OCaml version 4.01.1+dev2-2013-12-18+CLOSED

Cannot find file topfind.
Unknown directive `thread'.
# Printf.eprintf "hello world!";;
- : unit = ()
# 
$ cat /tmp/a.txt
hello world!

Use Printf.fprintf to print to Pervasives.stderr:

$ rlwrap ocaml 2>/tmp/a.txt
        OCaml version 4.01.1+dev2-2013-12-18+CLOSED

Cannot find file topfind.
Unknown directive `thread'.
# Printf.fprintf Pervasives.stderr "hello world!";;
- : unit = ()
# 
$ cat /tmp/a.txt
hello world!
answered Apr 12 by zma (2,200 points)

Please log in or register to answer this question.

Related questions

8 answers
asked Apr 6 by anonymous
0 answers
0 answers
0 answers
asked Apr 10 by anonymous
Welcome to Do This In Various Langs (dtivl), where you can ask questions and receive solutions in various programming languages.
Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...