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

  • Home
  • SEARCH
  • 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 5959581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:42:18+00:00 2026-05-22T18:42:18+00:00

I am running following test bash script: test.sh ======== pass=$1 if [ $pass -eq

  • 0

I am running following test bash script:

test.sh

========

pass=$1
if [ $pass -eq 1 ]; then
   exit 0
else
   exit 1
fi

=============

So, If I run ‘./test.sh 1’, it should give me success code, i.e. 0. And if I run ‘./test.sh 2’ it should give me specific error code, i.e. 1.

But when I run the script, I am getting 0 as exit code for both the cases.

Output

========================

# ./test.sh 1 |echo $?
  0
# ./test.sh 2 |echo $?
  0
#

=========================

What am I doing wrong here? Any help will be greatly appreciated!

Noman A.

  • 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-22T18:42:19+00:00Added an answer on May 22, 2026 at 6:42 pm

    Your script works, your test is broken though. Don’t use a pipe there.

    # ./test.sh 1 ; echo $?
    0
    # ./test.sh 2 ; echo $?
    1
    

    What you proposed with a pipeline cannot work, because all the processes in pipeline are started “simultaneously”. The shell starts a sub-shell to host each process (at least Bash does, implementations might vary -not sure about that), connects the input and output streams appropriately, then lets the OS schedule things as it sees fit.

    So the rightmost process (in your case echo $?) is started at the “same time” as your test script. Therefore $? in that sub-shell (which will have been expanded before the actual process is started) can’t possibly represent the return code from the test script – t.sh might not even have started yet!

    See the Wikipedia article on Unix Pipelines for some more information, or your shells documentation on pipelines. (Bash Pipelines for instance.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running following PHP code to interact with a MS Access database. $odbc_con
When running the following code it leaves out one row. When I do a
When running the following Java code, I get very accurate and consistent results in
I am currently running the following code based on Chapter 12.5 of the Python
Similar to this question , after running the following code the browser dialog does
I understand that using the === compares type, so running the following code results
When I am running the following statement: @filtered = map {s/ //g} @outdata; it is
I'm running the following query in Hypersonic DB (HSQLDB): SELECT (CASE foo WHEN 'a'
I tried running the following statement: INSERT INTO VOUCHER (VOUCHER_NUMBER, BOOK_ID, DENOMINATION) SELECT (a.number,
I have a production server running with the following flag: - XX:+HeapDumpOnOutOfMemoryError Last night

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.