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 $()?
You are in a sub-shell. The sub-shell exited.
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.
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.
Thanks for the pointer! I was looking in the "Command Substitution" section and the "FUNCTIONS" section. My bad.