mytest.sh
./mytest &
fg 1
sh mytest.sh
mytest.sh: 2: fg: No such job: 1
mytest.sh
./mytest &
fg 1
sh mytest.sh
mytest.sh: 2: fg: No such job: 1
fg
is a job control feature. Job control is turned off by default in scripts (and bash -- like other shells -- can be compiled to not have any interactive-only features at all; in NixOS, for example, this is the difference between the bash
and bashInteractive
packages -- so while in many Linux distros job control can be explicitly turned on in a script, this isn't true in all of them).
Wait by PID instead:
./mytest & mytest_pid=$!
wait "$mytest_pid"