Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • Should be $(exit 42); echo "$?" Commented Jul 14, 2015 at 4:35
  • 3
    @ElliotChance: The dollar sign in my answer represents the prompt and works as-is. What you propose would also work, but isn't necessary. Commented Jul 14, 2015 at 4:40
  • Note that you can use the range 0-255 but what actually happens is a modulo against 256. That means you can exit 300 and $? will be 44. Also, exit -1 would return 255 (a strange habit I've seen a lot). Just something to be aware of. It's best to just return the value you desire so stick to 0-255 so you (or someone else) won't get surprised.
    – boweeb
    Commented Mar 21, 2018 at 14:00
  • @boweeb -1 mod X == X-1 is pretty normal, you essentially continue the 0,1,...,X-1 sequence into negatives. See en.wikipedia.org/wiki/Modulo_operation for variations and rationale.
    – toolforger
    Commented Jul 3, 2019 at 5:30
  • @toolforger -- yup... that's what I said above. I'm not confused about the math. I'm confused why a developer would want to use a convoluted way to get to 255. My best guess is the convention implies the 255 return code isn't meant to be meaningful (and writing it as -1 is marginally easier to write(?)). RC 1 is common for a generic/unspecified error but I've never seen anything actually eval $? for 255 (specifically).
    – boweeb
    Commented Jan 29, 2020 at 13:33