How do i check for errors (and maybe warnings) in a bash script?
I want to precheck bash scripts before checking them into a git repository.
I know I can use bash -n script.sh
for a first, simple check but this doen't find for example things like rewriting on a readonly string.
readonly unique_id = "42"
unique_id = "21"
will be ok for a bash -n script.sh
but will fail on execution of course.
EDIT: the following script
#!/bin/bash
# check.sh
readonly unique_id="42"
unique_id="21"
give 0
if you execute bash -n check.sh; echo $?
but execution itself results in the error message ./check.sh: line 4: unique_id: readonly variable
Both behaviors are correct because this is not a syntax error but it will still break execution (even in unit tests) and I am asking if there is another way of precheck to filter out such errors.
Unittest will find only expected logical failures so this is not an alternative.