Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

According to the Open Group,

[t]he return utility shall cause the shell to stop executing the current function or dot script. If the shell is not currently executing a function or dot script, the results are unspecified.

However, if you run the following snippet

func () {
    ( return 1 )
    return 0
echo $?

the output is 0 (I tried bash and dash, with the same result). So, it seems that return does not cause the shell to stop executing the current function, which would contradict the POSIX standard. Am I missing something?

share|improve this question
up vote 7 down vote accepted
( return 1 )

This runs in a subshell. It does terminate the subshell immediately, and if you caught that shell's return code, it would be 1. The function itself returns 0 on the line after that.

(See Grouping Commands in the Shell Command Language specification.)

Compare with the {} form that doesn't introduce a subshell:

#! /bin/sh

func () {
    ( return 42 )
    echo "One:   $?"
    { return 1; }
    echo "Two:   $?"
    return 2

echo "Three: $?"

Output (note the absence of "Two"):

One:   42
Three: 1
share|improve this answer
Correct, however sometimes it's not obvious that return is executed in a sub-shell, for example if it's inside a while loop that reads input from a pipe. From the POSIX spec, I would have expected return to exit the function even if the return itself is executed in a sub-shell. – Ernest A C Sep 30 '12 at 14:24
There is absolutely no way for separate shell processes to communicate in the way you're describing. The only information returned to the shell from a subprocess are an exit code and the contents of standard output if you choose to capture it. – jgoldschrafe Sep 30 '12 at 16:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.