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!

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Great! That's exactly the solution to the problem, many thanks, @Dimitre Radoulov.
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!
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: stackoverflow.com/questions/6331075/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.