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 7967981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:00:21+00:00 2026-06-04T07:00:21+00:00

Could you guys help to give the algorithm used to generate $? in shell

  • 0

Could you guys help to give the algorithm used to generate $? in shell from exit code in program? For example,

  $? is 1 for exit(1);
  $? is 255 for exit(-1);

So I can infer exit code from $?

  $? is 1 => exit code is 1
  $? is 255 => exit code is -1

For something special,

  $? is 0 for exit(256);
  $? is 1 for exit(257);

Could anyone give the algorithm in shell used to generate $? with exit code, so that I can know the exit code only by observing $? after executing a command.

Thanks a lot.

Edit: To answer the question below, I added this example.

----a.c----
1 #include <stdlib.h>
2 int main()
3 {
4   exit(-1);
5 }
ning@m:~/work/02_test/ctest> gcc a.c
ning@m:~/work/02_test/ctest> ./a.out 
ning@m:~/work/02_test/ctest> echo $?
255
ning@m:~/work/02_test/ctest>
  • 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-06-04T07:00:22+00:00Added an answer on June 4, 2026 at 7:00 am

    With bash, $? is the exit code of the last command. Running a shell script .

    #!/bin/bash
    exit 113
    

    then echo $? shows 113. See http://tldp.org/LDP/abs/html/exit-status.html for more info

    With BASH there are some common exit codes.

    The return value is just an 8bit Int. Exit codes 255 means out of range, so -1. The reason you are seeing 256->0 and 257->1 is it’s wrapping around. Basically the exit value modulo 256.

    All of this is true with Bash or C

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

Sidebar

Related Questions

Could you guys give me an example how to read and write from/to xml
Hi guys wonder if you guys could help me I'm trying to compile a
Hey guys I was wondering if someone could provide a little help. I've been
Ok I give up and would really appreciate it if you guys could cast
Ok guys, could you tell me is there a certain bug in current code:
I'm new to jQuery, I was hoping you guys could help me. I'm trying
I am having compile errors. Could you guys help me get aroudn it. This
I'm learning NHibernate and hoping you guys could help me a bit about Tag
I would like to generate flow chart from a list of steps. For example
Could you guys help me I have a problem with deserialization via IXmlSerializable var

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.