Multiple exec in a shell script
The use of &
implie a sub-process.
So exec
have no effect.
Demo:
export LANG=Cecho $$17259exec sh -c 'echo $$;read foo' &[1] 1753817538[1]+ Stopped exec sh -c 'echo $$;read foo' fgexec sh -c 'echo $$;read foo' 17259
I run the script: echo $$;read foo
in order to prevent exit before having quietly read previous output.
In this sample, the current process ID is 17259
.
When run with ampersand (&
), the output is another pid (bigger). when run without ampersand, the new shell replace the command and is not forked.
Replacing the command by:
sh -c 'echo $$;set >/tmp/fork_test-$$.env;read'
re-running the whole test will generate two files in /tmp
.
On my desk, I could read:
197721999419772
So I found two files in /tmp
:
-rw-r--r-- 1 user0 user0 2677 jan 22 00:26 /tmp/fork_test-19772.env-rw-r--r-- 1 user0 user0 2689 jan 22 00:27 /tmp/fork_test-19994.env
If I run: diff /tmp/fork_test-19*env
, I read:
29c29< SHLVL='0'---> SHLVL='1'46a47> _='/bin/sh'
So the first run, with ampersand is in a sublevel.
Nota: This was tested under many different shell.