I want to use tee
to write the output of any command (first command)
to a file, but I want to get the return code from the first command
instead of the tee
command.
Example 1: The following code will return 1 and it is expected:
cp -unknown-args "hello"
echo "return code is $?"
---output---
cp: invalid option -- 'k'
Try 'cp --help' for more information.
return code is 1
Example 2: The following code will return 0 and it is unexpected:
cp -unknown-args "hello" | tee test.txt
echo "return code is $?"
---output---
cp: invalid option -- 'k'
Try 'cp --help' for more information.
return code is 0
I want to make "Example 2
" to return 1
if the first command
has any error.
Is it possible and how to do if yes?
try set -o pipefail
, this will propagate nonzero exits to later pipe members