This will cause the script to halt as expected, because grep a b
returns a non-zero exit code and the -e
option is set:
#!/bin/bash
set -e
grep a b
echo "hi"
$ ./test.sh
grep: b: No such file or directory
However, this doesn't, even though there is a syntax error:
#!/bin/bash
set -e
if [ $a = 'a' ]; then
echo "true"
else
echo "false"
fi
echo "hi"
$ ./test.sh
./test.sh: line 5: [: a: binary operator expected
false
hi
Is there a way to halt the script if there is an issue with the syntax passed to if [ ]
?