Bash Command That Prints a Message on Stderr

Bash command that prints a message on stderr

echo something >&2 is the correct way to do what you want.

However...

This will create a little program that will echo its arguments to stderr:

gcc -o echoerr -x c - <<'EOF'
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char **argv) {
int i;
for (i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]);
}
fprintf(stderr, "\n");
exit(0);
}
EOF

You can use it like this:

$ ./echoerr this is an error message
this is an error message
$ ./echoerr this is an error message 2>error.out
$ cat error.out
this is an error message

echo that outputs to stderr

You could do this, which facilitates reading:

>&2 echo "error"

>&2 copies file descriptor #2 to file descriptor #1. Therefore, after this redirection is performed, both file descriptors will refer to the same file: the one file descriptor #2 was originally referring to. For more information see the Bash Hackers Illustrated Redirection Tutorial.

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

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".

Bash print stderr only, not stdout

Just send the stdout to null:

cmd > /dev/null

This retains stderr, but suppresses stdout.

How to print on stderr without using /dev/stderr

To print to standard error, use a command that writes to standard output (like echo or printf) and redirect the output to file descriptor 2.

echo "This goes to standard output"
echo "This goes to standard error" >&2

This is the most common use of the file descriptor duplication operator, which makes the descriptor indicated by the preceding number (or 1 if omitted) a copy of the descriptor indicated by the following number.

How to print shell script stdout/stderr to file/s and console

I think, the easiest is to just add multiple files as arguments to the tee like this:

% python3 -c 'import sys; print("to stdout"); print("to stderr", file=sys.stderr)' 2>&1 | tee -a /tmp/file.txt /tmp/file_sec.txt
to stdout
to stderr
% cat /tmp/file.txt
to stdout
to stderr
% cat /tmp/file_sec.txt
to stdout
to stderr

Your script would look like this then:

#!/bin/bash

file=/tmp/file.txt
sec_file=/tmp/sec_file.txt

exec > >(tee -a "$file" "$sec_file") 2>&1

echo "hello world , we are very happy to stay here "


Related Topics



Leave a reply



Submit