1

The following line on a bash script:

$GEN_SDK $GEN_SDK_FLAGS $OUT_DIR/toolchain || panic "Cannot generate SDK toolchain!"

is panicking when executed. I believe the logic on this line is that if the first command runs OK, then the second one is not evaluated. But what does it mean for the first command to run OK? There's no such thing as return true in bash. So how does it know it didn't run OK?

terdon
  • 242,166
Gatonito
  • 145

1 Answers1

3

In the shell, all processes have an exit code (or return code). An exit code of 0 is "truthy" in that it indicates no error has occurred (this is inverse to what one usually expects in a programming language where 0 is usually "falsy"). The bash construct of command || othercommand will execute othercommand if the exit code of command is anything other than 0. This is comparable to the construct command && othercommand wherein othercommand will be executed if and only if the exit code of command is 0.

If you want to experiment with this, the shell expansion$? evaluates to the exit code of the previous command:

$ true; echo $?
0
$ false; echo $?
1
$ true || echo "Darned true."
$ false || echo "Lies."
Lies.
DopeGhoti
  • 76,081
  • I added set -x on the script and then copied the command $GEN_SDK $GEN_SDK_FLAGS $OUT_DIR/toolchain and exeucted it and it does not print 0 or 1 or anything, it just executes silently. Does it means its failing or not? – Gatonito Feb 03 '21 at 17:57
  • Return codes are not displayed; they are, as mentioned, stored in the magic shell variable ?. – DopeGhoti Feb 03 '21 at 18:29
  • @Gatonito the return status isn't printed. It will be saved in the special variable $?. Try $GEN_SDK $GEN_SDK_FLAGS $OUT_DIR/toolchain and then echo $?. If that prints 0 it worked, if it prints anything other than 0 there was a problem. – terdon Feb 03 '21 at 18:35