0

mytest.sh

./mytest &
fg 1
sh mytest.sh

mytest.sh: 2: fg: No such job: 1

  • Job control is turned off by default in scripts. `fg` isn't _expected_ to work in the first place. – Charles Duffy Sep 13 '22 at 16:46
  • ...while the error message you have is a bit different than the one described in some duplicates, that's presumably a `sh`-vs-`bash` difference. Job control being off-by-default in noninteractive interpreters is part of the POSIX spec, so it applies to all POSIX shells, not just bash. – Charles Duffy Sep 13 '22 at 16:53

1 Answers1

0

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"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441