title: How to return to bash prompt after printing output from backgrounded function?
body: <p>How can I return to my bash prompt automatically after printing output from a function that was put in the background?</p>
  
  <p>For example, when I run the following script in a bash shell:</p>
  
  <pre>
  fn(){
          sleep 10
          echo "Done"
          exit
  }
  fn &</pre>
  
  <p>After running the script, it immediately returns my prompt. After 10 seconds, it prints "Done" and then displays a blinking cursor on a new line:</p>
  
  <pre>
  $ Done
  ▏
  </pre>
  
  <p>The script isn't running anymore, but I don't get my prompt back until I press <kbd>Return</kbd>.</p>
  
  <p>Is there any way to force a return to the bash prompt after printing "Done"?</p>
  
  <p>A related question is: <a href="https://stackoverflow.com/questions/13227530/is-there-a-way-for-a-backgrounded-task-to-inform-the-terminal-to-print-a-new-pro">Is there a way for a backgrounded task to inform the terminal to print a new prompt?</a> However, that question asks about a backgrounded program. The answer supplied there applies to a <em>program</em> that is sent to the background, but doesn't seem to work for a <em>function</em> that is sent to the background (as in the example I supplied).</p>
  
  <p>To clarify: I am looking to save the entire code snippet above (e.g., as <code>myscript.sh</code>) and then run it as a foreground script (e.g., as <code>bash myscript.sh</code>).</p>
  
  <p><strong>EDIT</strong>: The above is of course just a MWE. The context of this problem is: </p>
  
  <ol>
  <li>User runs script</li>
  <li>Script submits PBS job, starts tailing the output file in the background, and calls <code>fn &amp;</code></li>
  <li>User gets prompt back, may start doing other things.</li>
  <li>Job output appears on user's terminal when job starts running</li>
  <li><code>fn</code> monitors the queue and kills <code>tail</code> when the job finishes.</li>
  <li>Users complain about not getting prompt back (i.e., having to press <kbd>Enter</kbd>) after this finishes.</li>
  </ol>
  
  <p>Here's some less minimal code:</p>
  
  <pre>
  watch_queue(){
      until [  `qstat | grep $job | wc -l` -lt 1 ]; do
          sleep 2
      done
      kill -9 $pid
      tput setaf 7
      tput setab 0
      echo "Hit ENTER to return to your command prompt."
      tput sgr0
      exit 0
  }
  
  cmd="something complicated that is built at runtime"
  outfile="ditto"
  queue="selected at runtime, too"
  
  job=`echo "cd \$PBS_O_WORKDIR  && $cmd >> $outfile " | 
       qsub -q $queue -e /dev/null -o /dev/null | 
       awk 'BEGIN { FS="." } { print $1 }'`
  
  echo "Job $job queued on $queue: $cmd"
  eval "tail -f -F $outfile 2>/dev/null &"
  pid=$!
  watch_queue &
  </pre>
  
  <p>Of course it would be a lot easier for me if my users could just pick up the job output from a separate file, or manipulate jobs between foreground and background on their own, but they can't. They can't even follow the instructions in the script to hit <kbd>Enter</kbd> to get the "look" of a prompt back... And I can't open another "window" - they do not have a display server.</p>