Code:
exec 3>&1
ls foo . 2>&1 >&3 | wc
Substitute your own command for "ls foo .", which is just an example of something that sends something to both stdout and stderr (assuming that there are some ordinary names in the current directory and that "foo" is not one of them).
The "exec 3>&1" creates file descriptor 3 as a copy of the
current fd 1. It's a temporary variable to save fd 1.
The ordering of the redirections on the next line is critical. The first thing processed is the pipe symbol separating the two commands, redirecting the first command's fd 1 to the pipe. Now, the redirections for the first command are processed from left to right. First, fd 2 is made a copy of the command's current fd 1, i.e., the pipe. Finally, fd 1 is made a copy of fd 3, which is the original stdout. The net result is that the command's stderr is sent to the pipe and stdout is sent to the same place it was going before all this redirection.