tee

standard out

df -h | tee disk_usage.txt

standard out and standard error

some_command 2>&1 | tee /tmp/out

Capturing return code

Capturing return code of command WHILE using tee

You cannot just capture return code as you typically would (Using [ "$?" -eq 0 ]). Since you will end up capturing the return code of tee which will be successful if tee was able to write to file.

Hence you need to use ${PIPESTATUS[0]}

some_func(){
  return 1
}

main() {
  some_func 2>&1 | tee /tmp/out
  return_code="${PIPESTATUS[0]}"

  echo "Return code from original command: ${return_code}"

  if [ "$return_code" -eq 0 ]; then
    echo_green "success"
  else
    echo_red "failure"
    return 1;
  fi
}

main "${@}" || exit 1


Children
  1. Capturing Return Code WHILE using `tee`