Timeline for Best practice to use $? in bash?
Current License: CC BY-SA 2.5
8 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jun 19, 2019 at 7:13 | comment | added | tripleee | @kmkaplan Wow, TIL! Thanks for taking the time to explain this. | |
| Jun 19, 2019 at 6:30 | comment | added | kmkaplan |
@tripleee No. command && true also prevents set -e from exiting the script if command returns a non-zero exit code. But it (mostly) does not change the exit code whereas command || true does.
|
|
| Jun 18, 2019 at 4:32 | comment | added | tripleee |
@kmkaplan Your last comment doesn't make any sense to me. The purpose of command || true is to prevent set -e from exiting the script if command returns a non-zero exit code. It changes the exit code because we need to. The only thing command && true does is run true (return a zero exit code) if `command succeeded (returned a zero exit code) - it's a complete no-op.
|
|
| Feb 5, 2014 at 7:49 | comment | added | kmkaplan |
@ShawnJ.Goff I prefer to do command && true. Like that the return value is not changed.
|
|
| Feb 5, 2014 at 7:46 | comment | added | kmkaplan | This is part of POSIX, not a bash specific feature. Use it but be aware of some pitfalls mywiki.wooledge.org/BashFAQ/105 | |
| Mar 8, 2011 at 18:41 | comment | added | Gilles 'SO- stop being evil' |
Furthermore, even if you use set -e, you can set an “exception handler” to catch all errors with trap did_it_work EXIT.
|
|
| Mar 7, 2011 at 18:49 | comment | added | Shawn J. Goff |
set -e is useful. There are some commands that return non-zero under normal circumstances (for instance, diff). When I'm using set -e in a script where I'm expecting a nonzero return, I do command || true.
|
|
| Mar 7, 2011 at 18:14 | history | answered | Steven D | CC BY-SA 2.5 |