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.

7
  • 6
    The [ command modifies $? too. Use: dat; status=$?; if [ $status = 0 ]; then echo "Success: $status"; else echo "Failure: $status"; fi. Commented Sep 8, 2020 at 12:11
  • 2
    The $? in the else clause is the exit status of [ $? -eq 0 ] in the if clause. Commented Sep 8, 2020 at 12:14
  • @ZumodeVidrio : Looks like a XY-problem to me. What your use case? What do you want to achieve? Commented Sep 8, 2020 at 13:19
  • @user1934428 No no, it's not an XY-problem at all, I was just trying to add some debug into a script and wanted to take into account the error code in case it happens. Commented Sep 8, 2020 at 14:14
  • Then, why only 0 and 127? Codes larger 127 are virtually always errors occuring when starting a process, and most programs use codes between 1 (or 2) and 126 for errors too. And why do you want to continue on failure? Wouldn't a set -e better fulfil what you want? Commented Sep 8, 2020 at 14:38