Bug 169420 - Doing "exit" in function doesn't terminate script
Summary: Doing "exit" in function doesn't terminate script
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Enterprise Linux 4
Classification: Red Hat
Component: bash
Version: 4.0
Hardware: All
OS: Linux
medium
medium
Target Milestone: ---
: ---
Assignee: Tim Waugh
QA Contact: Ben Levenson
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2005-09-28 06:27 UTC by Johan Walles
Modified: 2007-11-30 22:07 UTC (History)
0 users

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2005-09-28 12:17:23 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)

Description Johan Walles 2005-09-28 06:27:01 UTC
Description of problem:
"
#!/bin/bash

# The point of this program is to find out why the calls to exit in
# debugcore++.sh does not work.

# Answer: If the shell function is called using back quotes, it won't
# be able to exit the whole program.

function pleaseExit () {
    echo monkey
    exit
}

MONKEY=$(pleaseExit)
echo pleaseExit did not exit but returned: \"$MONKEY\"

# If we get here, something is wrong
exit 1
"

Version-Release number of selected component (if applicable):
All I have tried.  For example:
bash-3.0-19.2

How reproducible:
Every time

Steps to Reproduce:
1. Write a script with a function that does "exit".
2. Call the function using $(), i.e. FOO=$(yourFunction)
  
Actual results:
The script doesn't terminate.

Expected results:
The script should have terminated.

Additional info:
I have searched for information about this, but the only things I've managed to
come up with are indications that this should work as I expect:

Quoting the man page:
"Functions are executed in the context of the current shell; no new process is
created to interpret them".  Shouldn't this mean that "exit" should exit the
current shell?

There is a "return" command for returning from a function with a result code.
Why have "exit" do the same thing?

Doing "exit" in a function works as expected if you call the function without
$().  Why not with $()?

Comment 1 Tim Waugh 2005-09-28 08:32:33 UTC
You are in a sub-shell.  The sub-shell exited.

Comment 2 Johan Walles 2005-09-28 11:56:40 UTC
Neither the info documentation nor the man page says a word about command
substitution being performed in a sub-shell.  Are you sure about this?

AFAIU functions are supposed to be executed in the same shell as the caller
(which they are unless they are called with $()).

Feel free to re-close this if you are certain, but please say something about
where I can find this behaviour documented as well.


Comment 3 Tim Waugh 2005-09-28 12:17:23 UTC
Sure it does, in 'man bash':

       Command  substitution,  commands  grouped  with  parentheses, and asyn-
       chronous commands are invoked in  a  subshell  environment  that  is  a
       duplicate  of  the  shell  environment, except that traps caught by the
       shell are reset to the values that the shell inherited from its  parent
       at invocation.

Comment 4 Johan Walles 2005-09-28 12:37:29 UTC
Thanks for the pointer!

I was looking in the "Command Substitution" section and the "FUNCTIONS" section.
 My bad.



Note You need to log in before you can comment on or make changes to this bug.