In terminal based scripts (Shell, Bash, Python, etc), if you prompt the user "Do you wish to continue?" and the user chooses "No", what exit code should the script ideally return?

On one hand, the script executed as the user expected it to (the user wanted to quit, so it quit), so it should give an exit code of 0. On the other hand, the script was unable to complete its main task, which should be a non-zero exit code.

  • I wasn't sure if this question belonged here or elsewhere; this question seemed a bit too "abstract" for StackOverflow.
    – IQAndreas
    Sep 27, 2013 at 22:11
  • 2
    If its unable to complete its main task, you probably want an error exit code. Perhaps one that indicates specifically 'user cancelled'. Sep 27, 2013 at 22:16
  • @GrandmasterB - Is there a standard exit code for that, or would I need to pick one and indicate which number is used for that purpose in the documentation?
    – IQAndreas
    Sep 27, 2013 at 22:18
  • I really dont know if there's a standard one for something like that. So long as you have the codes documented I dont think it will make a lot of difference what they are specifically. Sep 27, 2013 at 22:26
  • Seems like a good idea. You should add your comments as an answer (and then we can remove them from here).
    – IQAndreas
    Sep 27, 2013 at 22:37

2 Answers 2


There is no real standard on this, other than "zero means keep going".

So it depends on what should typically happen in a situation where you do this:

$ your-script && do-something-else

...and the user cancels out in your-script. Is the desired behavior to skip that step and continue (i.o.w., is your-script completely optional)? Then a zero exit code is appropriate. But if, typically, continuing after the user has cancelled your script is inappropriate, then you should return non-zero. If in doubt, use non-zero: otherwise whoever calls your script won't be able to distinguish between "user cancelled out" and "user said yes and everything worked as intended" - discarding a non-zero exit code is easy, but figuring out what happened if the code is zero either way is not.

Just make sure that you use distinct exit codes for "user cancelled out" and "something went horribly wrong", and of course document them.


The script was able to complete its main task: respond without any error to the user. It's not like it had a permission issue, a segfault, a missing file, or something like this. It was the intended behavior to stop.

Errors should be used when unintended behaviors happen: the user wanted the program to do a task, and the program couldn't.

  • Exit codes are not necessarily error codes.
    – tdammers
    Sep 27, 2013 at 22:30
  • @tdammers well, if a program terminates with a code other than 0, it means the program did not terminate correctly. There is a reason for all the linux error codes to have this name. Sep 27, 2013 at 22:44
  • 1
    @FlorianMargaine - You are conflating syscall error numbers with process exit codes. They are not the same thing.
    – Stephen C
    Sep 27, 2013 at 23:20
  • man.cx/grep. Returning non-zero doesn't necessarily mean abnormal termination or unintended behavior.
    – tdammers
    Oct 8, 2013 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.