Hai Vu
Hai Vu

Reputation: 40763

What does `return -1 || exit -1` mean?

I'm confused by the following bash line, written by someone else:

return -1 || exit -1

What does it mean? I understand the || construct means if the first part (in this case, return -1) failed (i.e. return a non-zero code), then the second part (exit -1) is executed. What's also strange is this statement is not part of any function, but in the main body of the script.

I appreciate someone who explains this to me.

Upvotes: 8

Views: 9539

Answers (1)

heemayl
heemayl

Reputation: 42077

Here, return is a trick to exit when the script is source -ed and exit is usual that it will exit a shell.

So essentially the above condition is to exit from the execution loop of the script whether it is source -ed or executed.

Also note that, the negative return values are not supported in bash. In both cases, you would get exit status of 255, not -1.

Upvotes: 9

Related Questions