Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Cleanly exiting from a 'sourced' script... (bash)

3,034 views
Skip to first unread message

Kenny McCormack

unread,
Apr 16, 2013, 3:46:30 PM4/16/13
to
I know this is in the category of "If you need to do that, then you
shouldn't be sourcing it", but I'm asking anyway... I'm sure someone,
somewhere, has come up with a clever way to do this.

We all know that when you write a shell script that is intended to be run
"normally", that is, like a normal command, then you can use "exit" to bomb
out of the script (like, when an error occurs). But, if you do this in a
"sourced" script (that is, run via "." or "source" in bash), it terminates
the calling shell (this is, of course, entirely as expected, since the point
of "source" is that it is just like if you were typing the commands manually
into the calling shell).

So, my question is: Is there a way to cleanly exit from the sourced script -
that *doesn't* kill the parent shell?

--
Given Bush and his insanely expensive wars (*), that we will be paying for
for generations to come, the only possible response a sensible person need
ever give, when a GOPer/TeaBagger says anything about "deficits", is a
polite snicker.

(*) Obvious money transfers between the taxpayers and Bush's moneyed
interests. Someday, we'll actually figure out a way to have a war where the
money just gets moved around and nobody (on either side) gets injured or
killed. That will be an accomplishment of which we will be justly proud.

Chris F.A. Johnson

unread,
Apr 16, 2013, 4:30:04 PM4/16/13
to
On 2013-04-16, Kenny McCormack wrote:
...
> So, my question is: Is there a way to cleanly exit from the sourced script -
> that *doesn't* kill the parent shell?

return


--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

Kenny McCormack

unread,
Apr 16, 2013, 4:38:37 PM4/16/13
to
In article <cvo04a-...@cfa.johnson>,
Chris F.A. Johnson <cfajo...@gmail.com> wrote:
>On 2013-04-16, Kenny McCormack wrote:
>...
>> So, my question is: Is there a way to cleanly exit from the sourced script -
>> that *doesn't* kill the parent shell?
>
> return

Indeed. Thanks!

$ cat /tmp/mytesty
#!/bin/bash
echo one
return
echo two
$ /tmp/mytesty
one
/tmp/mytesty: line 3: return: can only `return' from a function or sourced script
two
$ . /tmp/mytesty
one
$

--
"Every time Mitt opens his mouth, a swing state gets its wings."

(Should be on a bumper sticker)

Janis Papanagnou

unread,
Apr 16, 2013, 4:43:44 PM4/16/13
to
On 16.04.2013 22:38, Kenny McCormack wrote:
> In article <cvo04a-...@cfa.johnson>,
> Chris F.A. Johnson <cfajo...@gmail.com> wrote:
>> On 2013-04-16, Kenny McCormack wrote:
>> ...
>>> So, my question is: Is there a way to cleanly exit from the sourced script -
>>> that *doesn't* kill the parent shell?
>>
>> return
>
> Indeed. Thanks!
>
> $ cat /tmp/mytesty
> #!/bin/bash
> echo one
> return
> echo two
> $ /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'.

Janis

Kenny McCormack

unread,
Apr 16, 2013, 7:18:26 PM4/16/13
to
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)

Janis Papanagnou

unread,
Apr 16, 2013, 7:56:04 PM4/16/13
to
On 17.04.2013 01:18, Kenny McCormack wrote:
[sourced script]
>
> 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 -

Ksh and Bash return some invocation flags with "$-". I could not find
anything documented; my [wild] guess would be that 's' indicates a
sourced file. Starting from an interactive shell I either get /imsBE/
if sourced, or /hB/ if called as executable with shebang.

Janis

David W. Hodgins

unread,
Apr 16, 2013, 9:19:25 PM4/16/13
to
On Tue, 16 Apr 2013 19:18:26 -0400, Kenny McCormack <gaz...@shell.xmission.com> wrote:


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

[dave@x2s ~]$ cat bin/testcaller2.sh
#!/bin/bash
printf '%s\n' "About to source testcaller"
. testcaller.sh
printf '%s\n' "About to execute testcaller"
testcaller.sh

[dave@x2s ~]$ cat bin/testcaller.sh
#!/bin/bash
result=$(caller)
printf '%s\n' "Return from caller is $result."

[dave@x2s ~]$ bin/testcaller2.sh
About to source testcaller
Return from caller is 3 bin/testcaller2.sh.
About to execute testcaller
Return from caller is 0 NULL.

Regards, Dave Hodgins

--
Change nomail.afraid.org to ody.ca to reply by email.
(nomail.afraid.org has been set up specifically for
use in usenet. Feel free to use it yourself.)

Martin Vaeth

unread,
Apr 17, 2013, 6:25:07 AM4/17/13
to
Kenny McCormack <gaz...@shell.xmission.com> wrote:
> 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.

You could put the whole script into a function which you call at
the end of the script. Then "return" always bahves as expected.

(Of course, this is also easier in zsh where you have anonymous functions.)

Barry Margolin

unread,
Apr 17, 2013, 12:26:06 PM4/17/13
to
In article <kkkm82$37h$1...@news.xmission.com>,
gaz...@shell.xmission.com (Kenny McCormack) wrote:

> 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).

Seems like it would be pretty unusual to need that. Scripts that are
sourced usually need to be, since the purpose is to set options and
variables in the calling shell. I suppose you could be clever and write
a script that could be used as either:

. scriptname

or:

scriptname command line

In the first case it sets variables in the calling environment, in the
second it sets them locally and then invokes the arguments as a command
line (inheriting the environment).

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Seebs

unread,
Apr 26, 2013, 2:51:24 PM4/26/13
to
On 2013-04-16, Chris F.A. Johnson <cfajo...@gmail.com> wrote:
> On 2013-04-16, Kenny McCormack wrote:
> ...
>> So, my question is: Is there a way to cleanly exit from the sourced script -
>> that *doesn't* kill the parent shell?

> return

So, I went and looked this up in my book, and it says that "some shells"
exist in which a return command outside of a shell function exits the entire
shell, not just the sourced script, but I didn't list which ones, and now
I don't remember.

I am pretty sure this is in some way ironic. It's not just that I'm looking
things up in my own book, it's that I'm now mad at the author for not giving
more details.

-s
--
Copyright 2013, all wrongs reversed. Peter Seebach / usenet...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
0 new messages