Search code examples
bashscriptingjobspidmultiprocess

Bash script: Suppress job creation message


I have a bash function that runs a few sub-processes in parallel, like this:

#!/bin/bash
function check() {
  set +m
  for f in foo bar ; do
    (
      if [ -f $f ] ; then ls -la $f >> all; fi
    ) &
  done
  wait
}

On sourcing and running this (. scriptfile; check), the +m has successfully suppressed job completion output, but it still shows process IDs on creation, like:

[1] 123
[2] 456

How could those ID lines be suppressed?


Solution

  • Shell writes background process-id on stderr so one way of doing it is to suppress stderr inside your script:

    #!/bin/bash
    function check() {
      set +m
      for f in foo bar ; do
        {
          if [ -f $f ] ; then ls -la $f >> all; fi
        } &
      done 2>/dev/null
      wait
    }