"Odd" behavior exiting shell script

Is it normal behavior for a shell script that terminates to terminate its parent shell when executed with the "." option?

For example, if I have the example script (we'll name it ex.sh):

#!/bin/sh
if [ $# -ne 1 ]
then
  echo "Bye."
  exit 2
fi

And I execute it like this:

>./ex.sh

It says "Bye." and then returns me to my terminal. If however, I execute it like this:

>. ./ex.sh

It acts the same as above, but my terminal session is terminated after it exits. Is this normal? If so, can I avoid it? It does not seem to matter what the exit status is.... The real script will be setting environment variables that the calling environment will need, so I need the affect of ". ./ex.sh" without the ill effect of terminating the calling shell if it fails for some reason (I want the caller to see the failure and act accordingly).

For what it's worth, the caller will be (.profile) which is why this is very undesirable, becasue if it fails then the user will never see why and never be able to log in (unless they su without the profile executing). Not good....

O/S is AIX 5.3 btw.

You are trying to execute the script in the same shell.

Use sh ./ex.sh to run it in a new shell, where the exit will get back to your original shell.

That won't work because then the environment variables I need to export won't get back to the original shell.

#!/bin/sh
if [ $# -ne 1 ]
then
  echo "Bye."
  exit 2
fi

The variable $# is the number of parameters provided to the script when calling the script.
When called from the command line as "ex.sh" with no parameters $# has the value zero which is not equal to one. The script then outputs "Bye" and exits. This does not log you out because the script was executed in a self-contained shell.

When called with ". ./ex.sh" the script is executed in the current shell. Again because the number of parameters supplied was zero the shell outputs "Bye" and then issues an "exit" to the current shell which logs you out.

Try providing exactly one parameter and see if the result is different:

./ex.sh myparameter
. ./ex.sh myparameter

I know all that, lol. I think maybe I should ask this succinctly as follows:

If I want to prematurely terminate a shell script, executed with the ". ./script.sh" syntax, how can I do it without terminating the calling shell?

Since there is no goto in ksh that I can use to get to the end of the script, I don't see how to stop it at "Bye" with out using exit or wrapping the whole script in the if/else block. The actual script I'm doing this with is much more complicated, this trivial script is just to show the behavior I was experiencing.

I suppose the answer is "this is normal, the script is executing under the context of the current shell". So, the next question is, how else to stop the script without exitting the shell?

if you're going to source the script in this way, use "return" instead of "exit". it'll return control back to the calling session.

that works! thanks.