Return value of piped command?

grep $SEARCH_STRING /etc/passwd | cut -d":" -f 1,5 

I need to check the $? value of grep in the above. If I place a test for $? after the above piped command, it returns success status of grep piped to cut.

How can I get the success status of grep alone?

Different shells have different mechanisms for dealing with this situation.

Bash 3.0+ and ksh93 have the pipefail option (set -o pipefail) which changes the exit code behavior of pipelines and reports the exit code of the pipeline as the exit code of the last program to return a non-zero exit code.

Bash has the PIPESTATUS array variable which contains a list of exit status values from the processes in the most-recently-executed foreground pipeline

zsh has pipestatus.

Another way is to use coprocesses.

I'm using ksh and the command set -o pipefail fails with the message

ksh: pipefail: bad option(s)

Can you please throw more light on coprocesses and the usage?

Then you are apparantly using either ksh88 or pdksh.

You to combine exit statuses logically, so that you can test more than one thing at a time.

statement1 && statement2 

means execute statement1 and if its exit status is 0 (sucessful) then execute statement2

statement1 || statement2

means execute statement1 and if its exit status is not 0 then execute statement2

Regards

But you can't do that in a pipeline.

Another makeshift solution would be to invoke a subshell and have it somehow smuggle out the exit status to a dedicated file descriptor. I don't think we want to go there, though.