In bash how do we make the script to automatically exit if a command line return code is not zero. For example:
#!/bin/bash
cd /something_something
mv file_a /somedir/file_a # this produce an error
echo $? # This produce a non-zero output
echo "We should not continue to this line"
I know we can debug bash script with #!/bin/bash -x but sometime the script is too long, it run so fast, and we missed important error.
And I don't want to keep writing
[[ $? -ne 0 ]] && run next_command
set -eor#!/bin/bash -emv file_a /somedir/file_a || exit?bash --helpdoesn't show this option, regardless, it works. Could you please move it as an answer?set -ehas lots of heuristics to try to guess at those cases, they're different between different shells (and releases of the same shell), and those differences lead to surprises/breakage.