I created the file test.sh
which contains ls -ltr & jobs
command. When I run it, it gives me the output of ls -ltr
, but for jobs
command it doesn't give me anything, not even an error.4
Whats wrong?
I created the file test.sh
which contains ls -ltr & jobs
command. When I run it, it gives me the output of ls -ltr
, but for jobs
command it doesn't give me anything, not even an error.4
Whats wrong?
jobs
is an interactive command -- it is not meant to be used from scripts, and doesn't do anything useful in a script (but it could plausibly do something useful in a shell function called from an interactive session; so disabling it in code isn't really appropriate, either).
To keep track of background jobs, collect their PID:s when you start them.
ls -ltr &
pid=$!
printf 'pid: %s' "$pid"