0

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?

Here 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!

Tiago Cardoso
  • 2,057
  • 1
  • 14
  • 33

1 Answers1

4

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
Dimitre Radoulov
  • 27,252
  • 4
  • 40
  • 48
  • Great! That's exactly the solution to the problem, many thanks, @Dimitre Radoulov. – Tiago Cardoso Aug 04 '11 at 16:50
  • Actually.. besides the grants, I also was calling the script wrongly. If I call without slash, it still come back to the prompt without bash. So... I need to always use the slash. Thanks for the explanation! – Tiago Cardoso Aug 04 '11 at 16:54
  • Knowing the answer, is easier to find out the reason behind it. I'll add it here for reference purposes in case someone else falls into the same question: http://stackoverflow.com/questions/6331075/why-do-you-need-dot-slash-before-script-name-to-run-it-in-bash – Tiago Cardoso Aug 04 '11 at 19:05