2

Bash 5.0 includes a new -f option for wait:[1]

j. The `wait' builtin now has a `-f' option, which signfies to wait until the
   specified job or process terminates, instead of waiting until it changes
   state.

What does wait -f $pid do as opposed to the default wait $pid? Under what conditions is the -f option needed?

5

The change description is accurate, but somewhat obscure since wait is generally thought of as waiting for a process to finish.

Try this:

sleep 60&
wait %1

then in another terminal,

kill -STOP ${pid}

replacing ${pid} with sleep’s pid (as output when it was put in the background). wait will exit, because the job’s state changed.

With -f, wait will wait for the job or process to really terminate; used above, it wouldn’t exit with kill -STOP, and would wait for the process to be resumed (kill -CONT) and finish running.

  • 1
    At least with bash 4.3, this wait-for-any-state-change behavior happens only if the shell is interactive. In a script, fortunately, bash behaves as other shells: wait for the process to terminate. – Gilles Jan 8 at 16:36
  • @Gilles is the behaviour in an interactive shell vs. a non-interactive one documented anywhere? help wait doesn't mention that difference. – Whymarrh Jan 8 at 17:22
  • @Whymarrh Not in the documentation of 4.3. I haven't checked if it's been fixed in 5.0. – Gilles Jan 8 at 17:56

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.