Interview Prep/Shell Scripting/ShellCheck, debugging, testing, portability and security

How do you test a script against multiple Bash versions and shells?

AdvancedConceptShellCheck, debugging, testing, portability and security
Answer

Run the test suite in containers with different interpreters:

for v in 4.4 5.0 5.2; do
  docker run --rm -v "$PWD:/w" -w /w "bash:$v" bash -c 'bash --version | head -1 && ./run-tests.sh'
done
docker run --rm -v "$PWD:/w" -w /w alpine sh -c 'sh ./entrypoint.sh --self-test'

The official bash:<version> images make it easy to catch features used beyond your minimum version (wait -n -p needs 5.1, namerefs 4.3, ${var@Q} 4.4). Declare the minimum version in the README and check it at runtime: (( BASH_VERSINFO[0] >= 4 )) || die "need Bash 4+".

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on ShellCheck, debugging, testing, portability and security