Bug 169420 - Doing "exit" in function doesn't terminate script
Doing "exit" in function doesn't terminate script
Status: CLOSED NOTABUG
Product: Red Hat Enterprise Linux 4
Classification: Red Hat
Component: bash (Show other bugs)
4.0
All Linux
medium Severity medium
: ---
: ---
Assigned To: Tim Waugh
Ben Levenson
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2005-09-28 02:27 EDT by Johan Walles
Modified: 2007-11-30 17:07 EST (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2005-09-28 08:17:23 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description Johan Walles 2005-09-28 02:27:01 EDT
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 04:32:33 EDT
You are in a sub-shell.  The sub-shell exited.
Comment 2 Johan Walles 2005-09-28 07:56:40 EDT
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 08:17:23 EDT
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 08:37:29 EDT
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.