Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7071959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:45:01+00:00 2026-05-28T05:45:01+00:00

How do I get the correct return code from a unix command line application

  • 0

How do I get the correct return code from a unix command line application after I’ve piped it through another command that succeeded?

In detail, here’s the situation :

$ tar -cEvhf - -I ${sh_tar_inputlist} | gzip -5 -c > ${sh_tar_file}  --  when only the tar command fails $?=0
$ echo $?
0

And, what I’d like to see is:

$ tar -cEvhf - -I ${sh_tar_inputlist} 2>${sh_tar_error_file} | gzip -5 -c > ${sh_tar_file}
$ echo $?
1

Does anyone know how to accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:45:02+00:00Added an answer on May 28, 2026 at 5:45 am

    Here is a general solution using only POSIX shell and no temporary files:

    Starting from the pipeline:
    foo | bar | baz

    exec 4>&1
    error_statuses=`((foo || echo "0:$?" >&3) |
            (bar || echo "1:$?" >&3) | 
            (baz || echo "2:$?" >&3)) 3>&1 >&4`
    exec 4>&-
    

    $error_statuses contains the status codes of any failed processes, in random order, with indexes to tell which command emitted each status.

    # if "bar" failed, output its status:
    echo $error_statuses | grep '1:' | cut -d: -f2
    
    # test if all commands succeeded:
    test -z "$error_statuses"
    
    # test if the last command succeeded:
    echo $error_statuses | grep '2:' >/dev/null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't seem to get the correct Unix epoch time out of this PHP
The following code doesn't seem to get the correct count..... var materials = consRepository.FindAllMaterials().AsQueryable();
With my code below I convert XML to mySQL and after that I search
I have written some code using jQuery to use Ajax to get data from
I have a batch file that is using the exit command to return an
How do I get the correct $USER if I run a shell script with
I am unable to get the correct title for this, please change the title
Does anyone know how to get the correct mouse position during a drag-and-drop operation
How would be correct to get the font elements in the table with color=#0000cc
whats the correct way to cache a object(DataSet) and then get it and when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.