linuxmacosbashjob-control

bash: cannot list backgrounded jobs when inside bash script


I want to see the backgrounded jobs when inside a bash script. Is this possible? To illustrate I run the command /usr/bin/experiment here but end up with no output.

$ cat /usr/bin/experiment
#!/bin/bash
echo $(jobs)

$ watch ls

[1]+  Stopped                 watch ls
$ /usr/bin/experiment

$ 

Solution

  • Your script launches a new shell, which does not see the active jobs of the original shell.

    If you run your script as follows:

    source /usr/bin/experiment
    

    you will get the output you are looking for