Quitting from a script, either sourced or not

This is a very simple problem, I am wondering why I can find no answer anywhere...

I have a script that can be run either sourced or not. This script has some place where it needs to quit execution (e.g., when an error is found)

If I "exit", the sourced call would exit the parent shell, but if I "return", the non-sourced call is not allowed.

How to solve this?

I am not sure what do you mean by sourced?

Using return should be analogous to exit in both cases...without the pesky side-effect of killing the parent.

What exactly is the issue? Have you traced the process with set -x? [embed set -x into each function in use to capture the flow across each subprocess...]

I cannot "return" from a non-sourced script

$ ./test
./test: line 2: return: can only `return' from a function or sourced script

---------- Post updated at 06:09 PM ---------- Previous update was at 06:08 PM ----------

sourced:

$ . ./test

not sourced:

$ ./test

Any script could run sourced, or not.

Why would you need to source the script?

If you do source it, there are implications which could only be overcome programmatically.

The simple solution is: Don't source the script. If you have to source it, then source it from another script, not from the login prompt (if your concern is that you get logged out when you "exit" the script). If you really have to source it from the shell, start a second shell before you source it.

The main purpose of the script is to set some environment variables, so I MUST source it. However, the same script also manage a set of choices for these variables (i.e., it can create or remove an "environment", list all the "environments", alias an "environment" and then set all the environment variables to the values specified by the particular "environment" chosen). In the "management" case, you do not need to source it, e.g.:

$ test add an_environment
$ test remove another_environment
$ . test set an_environment

can you post the sourced script?

It could be, but not necessarily.

From Shell Command Language

Regards,
Alister

---------- Post updated at 12:55 PM ---------- Previous update was at 12:41 PM ----------

Well, if you must source and it must not kill the parent shell, it cannot use exit. Refactor the script so that it does not require it.

If you want to get really ugly, you could wrap the sourcing with something like:

exit() { return $1; }
. script
unset -f exit

Although that will not work if the exit calls occur within a function (the sourced function will return but the sourced script will continue executing).

Regards,
Alister