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+".
More on ShellCheck, debugging, testing, portability and security
What is ShellCheck and how do you use it in a team?BeginnerHow do you disable a ShellCheck warning, and when is that acceptable?IntermediateName the ShellCheck warnings you see most and what each means.IntermediateHow do you debug a Bash script?BeginnerHow do you make `set -x` output more useful with `PS4`?IntermediateWhat is bats, and how do you test a script that calls external commands?Intermediate