Odd behavior with simple bash shellscript exit
I'm start to playing around with ShellScript Unix, so maybe it's a silly question. Apologies if that's the case.
I was trying to handle the exit codes to properly address adverse situations in my code, and for this, I created a code snippet to understand the unix exit behavior. Here it is:
#!/usr/bin/bash
RES=1
if [ $RES -eq 0 ]
then
echo "Finishing with success!"
exit 0
else
echo "Finishing with error!"
exit 1
fi
I understood that, once the code is called (and terminated) I'd go back to bash prompt. However, it seems the exit instruction is also leaving bash. Is it normal? Maybe it's something related to my development environment?
Her开发者_开发百科e are the messages...
bash-3.00$ . errtest.sh
Finishing with error!
$ echo $?
1
$ bash
bash-3.00$ which bash
/usr/bin/bash
For reference, I've added the return and the bash location. Hope it helps.
Thanks in advance!
This is because you're sourcing the script in your current environment (by using the . command). Try executing the script with either:
bash ./errtest.sh
or by giving the necessary permissions to the script file and executing it like this:
chmod u+x ./errtest.sh
./errtest.sh
精彩评论