If I understand what you want, tee command is the solution:
your_command 2>&1 | tee /dev/stderr
The output error of your command (ie. date) is redirected to the standard output (with 2>&1). The result is doubled with tee command.
- One to standard output;
- The second to the file in parameter:
/dev/stderr
Then, the result with date command is:
> date 2>&1 | tee /dev/stderr Sun Dec 11 16:33:35 CET 2022 Sun Dec 11 16:33:35 CET 2022
So, you could redirect with > / >> and 2> / 2>> to any files you want:
> date 2>&1 | tee /dev/stderr >file1 2>file2 > cat file1 Sun Dec 11 16:40:59 CET 2022 > cat file2 Sun Dec 11 16:40:59 CET 2022
If an error occurs, with a bad command like datex:
> datex 2>&1 | tee /dev/stderr >file1 2>file2 > cat file1 bash: datex: command not found... > cat file2 bash: datex: command not found...