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

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


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.

| improve this answer | |
  • 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 'SO- stop being evil' Jan 8 '19 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 '19 at 17:22
  • @Whymarrh Not in the documentation of 4.3. I haven't checked if it's been fixed in 5.0. – Gilles 'SO- stop being evil' Jan 8 '19 at 17:56
  • @Whymarrh The online manual says "when job control is enabled", which by default is the case for an interactive shell. gnu.org/software/bash/manual/html_node/… – Roland Weber Jun 12 '19 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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