I have a complicated bash script so I broke it down to the code that matters:
#!/bin/sh
foo()
{
echo test
# here I do some work and if it fails I want to stop execution
exit 1
}
echo $(foo)
echo help
This is about my script and I would like it to print test
and then exit but the output is:
test
help
This is also true for bash
.
My temporary workaround at the moment is to replace the exit 1
with kill $$
.
What do I have to do so it does really exit?
Update:
- Just to clarify, only calling
foo
does not solve my problem - Someone suggested this: https://unix.stackexchange.com/questions/48533/exit-shell-script-from-a-subshell/48550#48550
- @rici suggested using a temporary file which is not much better than killing the process (at least not in my script), but it is usable
If someone has better suggestions, please speak out!
The
exit
builtin command causes the currently executing subshell to terminate. Since the command substitution syntax$(command)
creates a subshell to executecommand
, theexit
only terminates that subshell, not the shell which includes the result of the command substitution.If you really need command substitution, you're not going to be able to trap
exit
from the substituted command. In fact, you cannot even trap failure. About the only option you have is to send the output of the command into a temporary file and then substitute the temporary file in a subsequent step.In the simple case in the OP, you can just call
foo
directly instead ofecho $(foo)
. But if you needed to capture the output offoo
in a shell variable, for example, you could do it as follows