Question: I'm debugging an oddity with a shell script and wondering whether I've misunderstood how Bash's "fork" (`&`) works, perhaps because I usually use it in single commands through a terminal, e.g.:

```
[~me]$ someExecutable &

```

Now, in a shell script:

```
foo() {
   echo "logic"
}

bar() {
   echo "idic"
}

baz() {
   echo "katra"
}

foo &
bar
baz

```

My intention is for the function `foo` to be invoked asynchronously, and then for the remainder of the script to continue executing in the original process context.

When I execute this example script I *do* get the expected output:

```
logic
idic
katra

```

(that is, only single apparent calls to `bar` and `baz`; I'm not at all bothered by the relative *order* of the three lines of output, which I understand may vary)

but I may be misunderstanding both the output and the script itself. If so, that would certainly explain the oddity I'm seeing in my real code, and save me from having to dig any further.

Is it possible that, in the child process, after the call to `foo`, the forked shell script will continue? Meaning that the remainder of my shell script will be executed *twice*?

**What really happens when I use `&` like this in a Bash script?**

Answer: 
The ampersand is only forking the call to foo() in your example. It is not forking the entire script. Only foo() runs in a separate thread, which exits when foo() finishes. bar() and baz() continue to run in the same thread
Aside from the fact that the control operator `&` causes the command to be executed asynchronously in a subshell [1](http://www.gnu.org/software/bash/manual/bashref.html#Lists), you could make use of [Coprocesses](http://www.gnu.org/software/bash/manual/bashref.html#Coprocesses) to control the output.

Instead of invoking the function by saying:

```
foo &

```

say:

```
coproc fubar { foo; }

```

and then read the output by saying:

```
cat <&"${fubar[0]}"

```

This would also permit you to control the output (whether it needs to be emitted after or before other functions like `bar` or `baz`).

---

For example, the following:

```
foo() {
   echo "logic1"
   echo "logic2"
}

bar() {
   echo "idic"
}

baz() {
   echo "katra"
}

coproc fubar { foo; }
bar
baz
cat <&"${fubar[0]}"

```

would produce:

```
idic
katra
logic1
logic2

```