11

I've "inherited" some shell scripts for Linux machines running the GNU "bash" shell. In one particular case, the machine runs GNU bash version 2.0.5b

One of those scripts has a wait & ("wait ampersand") instruction as part of the "for line" of a for loop. At first sight, that seems to be a curious/interesting idiom, but my web searches for it did not return anything relevant. man wait shows the "BASH_BUILTINS" ("BASH BUILTINS COMMAND") manpage, which has the following description:

wait [n]  
  Wait for the specified process and return its termination status.
  n may be a process ID or a job spec­ification;  if  a  job spec is given,
  all processes in that job's pipeline are waited for.  If n is not
  given, all currently active child processes are waited for, and the 
  return status is zero. If n speci­fies a non-existent process or job, 
  the return status is 127.  Otherwise, the return status is the exit 
  status of the last process or job waited for.

By reading that part of this manpage, it seems to me that wait & is silently (in the background) making sure that "all currently active child processes are waited for, and the return status is zero.". Am I right in this interpretation? Is this a common and/or useful idiom?

For added context, I'm talking about the following kind of usage in the script:

for file in `ls *.txt ; wait &`
do
   ...
   [cp instructions]
   ...
   [mv instructions]
   ...
   [mailx instruction]
done
14

I can't imagine any reason to write this code, and I'm not quite sure what the person who wrote this code was trying to achieve. wait here does nothing -- from its perspective, there are no child processes, so it will just exit immediately and basically act as a noop (wait itself executes in a child process due to command substitution, but that's unrelated).

As an aside, parsing the output of ls is quite fragile. Instead, consider just doing this:

for file in *.txt; do
    ...
done

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.