In article <kkkd5l$bhi$
1...@news.m-online.net>,
Janis Papanagnou <
janis_pa...@hotmail.com> wrote:
...
>> $ /tmp/mytesty
>> one
>> /tmp/mytesty: line 3: return: can only `return' from a function or
>sourced script
>> two
>
>Interesting. Zsh and Ksh return without error after printing 'one'.
Indeed. It is actually too bad that bash doesn't do this (the intelligent
thing) as well. It makes it difficult to write a script that can work in
either mode (i.e., either as an executable or as a source file).
Notes:
1) If you do "set -e", then using "return" will work in either mode
(although you will get the ugly error message - when you run it in
executable mode).
2) If you could tell which mode you are in (and I think there is some
variable or something in bash that tells if you are in a source script -
I'll have to re-check the man page on that one...), then you could do:
if [ $ImBeingSourced ]; then return
else exit
fi
--
Religion is regarded by the common people as true,
by the wise as foolish,
and by the rulers as useful.
(Seneca the Younger, 65 AD)