Does the last line of command in the bash shell script pass the exit code to the parent?… here is a solution to the problem.
Does the last line of command in the bash shell script pass the exit code to the parent?
I have a bash script below that basically does something and calls diff
at the end. I wonder if bash handles exit codes in the same behavior as diff
. Since this script is called by another script, I wonder if I need to do any kind of error handling to handle diff's
exit code and then exit?
#!/bin/bash
# this is test.sh
# do something
# last line of the file:
diff -r $RUN_DIR/testdata/actual_result $RUN_DIR/testdata/expected_result
Here’s another script that calls above:
# this is run_test.sh
if /bin/bash "${dir}/test.sh"; then
echo "SUCCESS: ${dir}"
else
echo "FAILED: ${dir}"
# do not exit here, we continue with other unit tests
fi
Solution
If the diff
command is the last command executed in that bash file, then yes, its return value will be the return value of the bash script.
The alternative is to return an explicit return value via the exit
command, such as export number 113
. Execution will always terminate after the exit
call, so you can be more flexible….
To understand these things, it’s best to start reading the “man page”: man bash
….