I have a probably pretty easy beginner question:
How do I echo
from a shell script into both stdout and stderr?
I know that I can echo to stderr echo "foo" 1>&2
but I need the output in both.
I tried some Googling but nothing worked.
Either use this construct: cmd >>file. txt 2>&1 where >> file appends the output to the file and 2>&1 redirects the stderr to stdout .
These three special file descriptors handle the input and output from your script or a process. while STDIN or Standard Input helps the Linux Process or a script to get its input from the user ( typing in the keyboard) the Standard output STDOUT and Error STDERR helps to display the result to the user on the monitor.
The echo command writes text to standard output (stdout). Some common usages of the echo command are piping shell variable to other commands, writing text to stdout in a shell script, and redirecting text to a file.
The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.
This should do it
echo "foo" | tee /dev/stderr
echo foo | tee >(cat >&2)
This will work even if stderr is not supported.
Note: Soren's answer will not work if you have su'ed to another account that does have write permissions to the tty; but mine will.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With