jobs command behaving differently in script

Here is my test script:

#!/bin/sh
result=`jobs`
echo "
Jobs:
"$result

result=`ls`
echo "
LS
"$result

Here is the output:

Jobs:

LS
0 1 2 3 4 5 6 7 gcd initialize.sh #inter_round_clean.sh# inter_round_clean.sh inter_round_clean.sh~ look parallel_first_run.sh parallel_ith_run.sh README.txt test testscript.sh testscript.sh~

But when I enter "jobs" from the command line I get this:
[2] Running emacs inter_round_clean.sh &
[3]- Running emacs ../todo.txt &
[5]+ Running emacs testscript.sh &

Why isn't jobs behaving the same was as ls?

p.s. I tried the exact same thing but with $() instead of backticks and got the same result.

Are you actually logging in with /bin/sh or are you perhaps getting a better shell?

You didn't run any jobs from your "test" script.

They are owned by its parent shell.

Source your script from the command line to see them:

$ . ./testscript.sh
1 Like

Thanks, that fixed it and I understand my oversight.