Is it possible to keep the last command exit status (
$?) unaltered after a test?
E.g., I would like to do:
command -p sudo ... [ $? -ne 1 ] && exit $?
exit $? should return the sudo exit status, but instead it always returns
0 (the exit code of the test).
Is it possible to do that without a temporary variable?
Another example to clarify further:
spd-say "$@" [ $? -ne 127 ] && exit $?
In this case i want to exit only if the first command is found (exit code !=
127). And i want to exit with the actual
spd-say exit code (it may not be
EDIT: I forgot to mention that i prefer a POSIX-complaint solution for better portability.
I use this construct in scripts where i want to provide alternatives for the same command. For instance, see my crc32 script.
The problem with temporary variables is that they could shadow other variables, and to avoid that you must use long names, which is not good for code readability.