0

Why does a program named sl (stream locomotive) always end with an exit status of "1"?

sl

Animation sl now running...

$ echo $?
1

Why not zero exit status?

Can I get an explanation? I am running it in tmux.

3
  • 1
    I can't reproduce this. I tested on an Ubuntu system I have access to and the exit status was 0. In any case, this is just a little toy program, and the developers are free to have it exit with whatever code they want. Maybe it is failing on your system and exiting with 1, or maybe the implementation you have on your system was written to always exit with 1, only the developers will know for sure. Commented Sep 25, 2024 at 18:10
  • So, thanks for the explanation man, I tested this program on Termux maybe I will try it on ubuntu. I needed this explanation because I am a tester. Thanks for trying Commented Sep 25, 2024 at 18:15
  • 1
    Ah! That is an important detail. Using tools like termux could very well make a difference to something like sl that tries to take over the terminal. Please add that to your question. Commented Sep 25, 2024 at 18:25

1 Answer 1

6

It's a toy program, so normal design practices don't necessarily apply. But it's open-source and the history is public, so you can look at the source. Once you've found the relevant file and line, given that the project is maintained with Git, you can use git blame to find when that line was last modified. The project is on GitHub, so you can use the “Blame” button there.

The current version (5.03+) of the original project always exits with status 0.

Previous versions (until April 2017) exited with status 0 if you compiled them with a C99 compiler. If you compiled them with a non-C99 compliant compiler, they would, on most architectures, typically end up returning the low 8 bits of whatever value was returned from the last function call done from main. Or maybe not — in versions of the C language before C99, falling off the end of main was undefined behavior. In theory, undefined behavior can cause demons to fly out of your nose, although in a Unix environment, this is constrained by the availability of the proper device and the permission to access it. In practice, on most architectures, the return value from a function that returns int is placed in a register, so the exit code of the program would be calculated from whatever value was last written to that register, which tends to be the value returned by the last non-void function call (but might not be, depending on what the exact functions are and on the compiler optimizations).

There is a somewhat prominent fork whose author decided to always return 1 “Because when you type sl you technically failed”.

2
  • Ah, that last line is probably what the OP saw, good find! And also why I have had alias sl=ls in my ~/.bashrc all these years. Commented Sep 25, 2024 at 19:07
  • It kinda makes sense, as if you were to evaluate ls's exit status in order to do something on success, well, in that case the mispelt sl should better fail. Commented Sep 25, 2024 at 23:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.