Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

command does not return exit status due to tee

Shell Programming and Scripting

exit status, shell script, shell scripting, tee, unix scripting, unix scripting basics

Thread Tools Search this Thread Display Modes
Old Unix and Linux 12-05-2008
anand_bh anand_bh is offline
Registered User
Join Date: Apr 2008
Last Activity: 29 October 2015, 2:33 PM EDT
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Tools command does not return exit status due to tee


I am using /bin/sh. I want to display the stdout and stderr on the terminal as well as save it in a file, so I'm using this command.

gmake all 2>&1 | tee log

But even if gmake fails, it's always giving 0 as exit status, i suppose because of tee.

# false 2>&1 | tee Log
# echo $?

I was expecting non-zero value above. Any solution to this problem?

What if I want to use tee as well as get correct exit status?
Sponsored Links
Old Unix and Linux 12-05-2008
zaxxon's Unix or Linux Image
zaxxon zaxxon is offline Forum Staff  
code tag tagger
Join Date: Sep 2007
Last Activity: 26 September 2017, 2:49 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,572
Thanks: 175
Thanked 560 Times in 481 Posts
The exit status in your case will always be that of tee and tee seems successful, so you get a zero. You would have to get it before you hand it over to tee. So maybe you are better off to separate both, ie. redirect output in the known way and check the exit status of the command producing and have a look at the written log with tail -f for example.


make all >> make_all.out 2>&1; echo $?

# in another terminal session:
tail -f make_all.out

Sponsored Links
Old Unix and Linux 12-05-2008
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
Join Date: Feb 2005
Last Activity: 25 September 2017, 4:37 PM EDT
Location: Foxborough, MA
Posts: 8,191
Thanks: 226
Thanked 782 Times in 730 Posts
from USENET comp.unix.shell FAQ:


13. How do I get the exit code of cmd1 in cmd1|cmd2

    First, note that cmd1 exit code could be non-zero and still don't
    mean an error. This happens for instance in

    cmd | head -1

    you might observe a 141 (or 269 with ksh93) exit status of cmd1,
    but it's because cmd was interrupted by a SIGPIPE signal when
    "head -1" terminated after having read one line.

    To know the exit status of the elements of a pipeline
    cmd1 | cmd2 | cmd3

    a. with zsh:

       The exit codes are provided in the pipestatus special array.
       cmd1 exit code is in $pipestatus[1], cmd3 exit code in
       $pipestatus[3], so that $? is always the same as

    b. with bash:

       The exit codes are provided in the PIPESTATUS special array.
       cmd1 exit code is in ${PIPESTATUS[0]}, cmd3 exit code in
       ${PIPESTATUS[2]}, so that $? is always the same as
       ${PIPESTATUS: -1}.

    c. with any other Bourne like shells

       You need to use a trick to pass the exit codes to the main
       shell.  You can do it using a pipe(2). Instead of running
       "cmd1", you run "cmd1; echo $?" and make sure $? makes it way
       to the shell.

       exec 3>&1
       eval `
         # now, inside the `...`, fd4 goes to the pipe
         # whose other end is read and passed to eval;
         # fd1 is the normal standard output preserved
         # the line before with exec 3>&1
         exec 4>&1 >&3 3>&- 
           cmd1 4>&-; echo "ec1=$?;" >&4
         } | {
           cmd2 4>&-; echo "ec2=$?;" >&4
         } | cmd3
         echo "ec3=$?;" >&4

    d. with a POSIX shell

       You can use this function to make it easier:

       run() {
         while eval "\${pipestatus_$j+:} false"; do
           unset pipestatus_$j
         j=1 com= k=1 l=
         for a; do
           if [ "x$a" = 'x|' ]; then
             com="$com { $l "'3>&-
                         echo "pipestatus_'$j'=$?" >&3
                       } 4>&- |'
             j=$(($j+1)) l=
             l="$l \"\$$k\""
         com="$com $l"' 3>&- >&4 4>&-
                    echo "pipestatus_'$j'=$?"'
         exec 4>&1
         eval "$(exec 3>&1; eval "$com")"
         exec 4>&-
         while eval "\${pipestatus_$j+:} false"; do
           eval "[ \$pipestatus_$j -eq 0 ]" || return 1
         return 0
       use it as:
       run cmd1 \| cmd2 \| cmd3
       exit codes are in $pipestatus_1, $pipestatus_2, $pipestatus_3


Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Equivalents of tee command to find exit status of command vibhor_agarwali UNIX for Advanced & Expert Users 7 06-03-2011 01:16 AM
Logins command exit status is 236 not 0 parkea2 HP-UX 2 11-27-2009 04:16 AM
Return of EXIT status ( $? ) ZINGARO HP-UX 1 03-12-2008 07:07 AM
exit status of command in a pipe line topcat8 UNIX for Dummies Questions & Answers 10 10-19-2007 04:39 AM
Getting the exit status of a remote command zoonalex Shell Programming and Scripting 1 08-23-2006 04:58 PM

All times are GMT -4. The time now is 03:52 PM.