I have a bash function that returns an exit status. How does one use the function taking arguments and returning an exit status, in an if
construct?
Asked
Active
Viewed 55 times
0

Dilna
- 405
- 1
- 6
-
2Does this answer your question? [How to check the exit status using an if statement](https://stackoverflow.com/questions/26675681/how-to-check-the-exit-status-using-an-if-statement) – Aserre Nov 30 '21 at 15:09
-
`if myfunction arg1 arg2; then echo success; else echo failure; fi` Alternatively, use `$?`, which is the exit code of the last command: `myfunction arg1 arg2; if [[ $? -eq 0 ]]; then echo success; else echo failure; fi`. Remember that new lines can be used in place of semicolon (`;`). – dan Nov 30 '21 at 15:21
-
Does the function use `exit` or `return`? – Fravadona Nov 30 '21 at 15:25
-
It uses return 0 for success, whilst return 1 and greater for error. – Dilna Nov 30 '21 at 15:48
-
Every command has an exit status, and arguments can be part of any simple command (whether it uses them or not). What is it that you think is distinctive about the case you have in mind, that does not apply to all uses of an `if` construct? – John Bollinger Nov 30 '21 at 16:38
-
Is `$?` the only way to get the exit status of a function? – Dilna Dec 01 '21 at 06:09