I have set -e
turned on for my script. The only thing is there is one command here that I don't want causing the script to exit if it fails, but I want everything else to do that. How can I keep set -e
on, and not have my script exit when an error code is thrown?
script in question:
native=$(pacman -Qenq -)
If stdin has a non-native package name an error code gets written to stdin.
Best Answer
set -e
akaset -o errexit
doesn't apply to commands that are parts of conditions like in:That also applies to the
ERR
trap for shells supporting it.So, an idiomatic way to ignore the failure of a command is with:
Or just:
That cancels
set -e
for thatcmd
invocation and also sets$?
to 0 (to that of:
/true
whencmd
fails)Also cancels
set -e
but preserves the exit status ofcmd
.