Bash style

Bash is frequently used in our build, CI and deployment systems. Some general guidelines and recommended reading

Reading from a file

Prefer using mapfile instead of while IFS to read a file

mapfile -t myArray < file.txt
mapfile -t myArray < <(find -d .)

instead of

while IFS= read -r line
  echo "$line"
done < "$input"

Set -eu -o pipefail

This is generally “bash strict mode” and sets

  • e exit if error
  • u error on variable unset (and exit)
  • -o pipefail fail if items in the pipe | fail. Bash otherwise continues if error | pass which causes some unexpected behavior.

Recommend using these at the start of all scripts and specifically disabling if a section of a bash script does not need them (for example, you want to let a pipe fail).

Utilities in use

We use shfmt and shellcheck for our shell script linters