If by sequence you mean pipe then you need to set pipefail
in your script like set -o pipefail
. From man bash
:
The return status of a pipeline is the exit status of the last command,
unless the pipefail option is
enabled. If pipefail is enabled, the pipeline's return status is the value of the last (rightmost)
command to exit with a non-zero status, or zero if all commands exit successfully. If the reserved
word ! precedes a pipeline, the exit status of that pipeline is the logical negation of the exit
status as described above. The shell waits for all commands in the pipeline to terminate before
returning a value.
If you just mean sequential commands then just check the exit status of each command and set a flag if the exit status is none zero. Have your script return the value of the flag like:
#!/bin/bash
EXIT=0
grep -q A <<< 'ABC' || EXIT=$? # Will exit with 0
grep -q a <<< 'ABC' || EXIT=$? # Will exit with 1
grep -q A <<< 'ABC' || EXIT=$? # Will exit with 0
echo $EXIT # Will print 1
exit $EXIT # Exit status of script will be 1
This uses the logical operator OR ||
to only set EXIT
if the command fails. If multiple commands fail the exit status from the last failed command will be return by the script.
If these commands are not running in the background then wait
isn't relevant here.