I recently discovered that a failed build deployed to production on a site I was working on. Here is a high level overview of the script.
#!/bin/bash build deploy
The issue here is that if build fails, then the script will still deploy then exit successfully (i.e. with an exit code of 0).
You can avoid this issue using Bash Unofficial Strict Mode. Here is the same script using Bash Unofficial Strict Mode
#!/bin/bash set -euo pipefail IFS=$'\n\t' build deploy
Let’s walk through the second and third lines.
set -euo pipefail
Command by command
Exit immediately if any command has a non zero exit status
Exit immediately if referencing any variable that has not been previously defined
set -o pipefail
Prevent errors in a pipeline from being masked (e.g. if you are piping the results from command1 to command2 and there is an error when running command1 then the pipeline will exit with that error).
In Bash, there is no string splitting function. You need to use IFS (Internal Field Separator) to control string splitting.
This will split on new lines rather than splitting on spaces in strings. This makes sense in a lot of cases but is not the default in Bash. The default value of IFS is: space, tab, newline.
Save yourself a lot of headache and use Bash Unofficial Strict Mode by default.