Bourne Shell: Force STDOUT for STDERR

I am creating a Subversion post-commit hook . I need to force one command to be output to STDERR so that the hook, after committing, sends a message back to the client.

How to force STDOUT to use STDERR?

In this simplified example, a file exists foo, but bardoes not work.

# touch foo
# ls foo bar
ls: bar: No such file or directory
foo

I want to send STDOUT to STDERR. I assumed I could do this with help >&2, but it does not seem to work.

I was expecting the following example of redirecting STDOUT to STDERR, and that /tmp/testwould contain the output and error for the command. But instead, it seems that STDOUT is never redirected to STDERR, and therefore /tmp/testonly contains an error from the command.

# ls foo bar >&2 2>/tmp/test
foo
# cat /tmp/test
ls: bar: No such file or directory

What am I missing?

CentOS, Unbuntu, FreeBSD MacOSX.

+3
2

, . , :

# ls foo bar >&2 2>/tmp/test

FIRST stdout stderr ( , - , ), THEN stderr /tmp/test. , stdout , stderr, .

# ls foo bar 2>/tmp/test >&2

stderr FIRST THEN stdout .

+9

STDOUT 1, . 1.

$ ls bar 1>&2 2>test
$ cat test
ls: cannot access bar: No such file or directory

man bash, REDIRECTIONS: -

Appending Standard Output and Standard Error
   This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be appended to the
   file whose name is the expansion of word.

   The format for appending standard output and standard error is:

          &>>word

   This is semantically equivalent to

          >>word 2>&1
-2

All Articles