I am learning how to efficiently use different set
options in my script and came across set -u
that appears to be perfect for exiting my script if a variable does not get set properly (ex. deleting users). According to the man page, set -u
and set -e
does the following...
-e Exit immediately if a command exits with a non-zero status.
-u Treat unset variables as an error when substituting.
I created a test script to test this functionality, but it does not appear to be working as expected. Perhaps someone could better explain my issue to me and where I am mis-interpreting? Test script is below. Thank you.
set -e
set -u
testing="This works"
echo $?
echo ${testing}
testing2=
echo $?
echo ${testing2}
testing3="This should not appear"
echo $?
echo ${testing3}
I expect the script to display 0 and "This works", and then fail as ${testing2}
is not set.
Instead I am displayed 0 and "This works", follow by 0 and then 0 This should not appear
Can anyone provide some knowledge? Thank you.