I have a while loop to count to 5 and echo "Done" when finished. I want to be able to execute the script in the background with "&" and continuing issuing commands while it runs.

But when it's done, I want it make itself the foreground process before echoing "Done". After that, I would expect it to show a new command prompt line. I can just echo "\nDone", and I understand why running it in the background would just print the output and not end/start a new prompt. But I'm wondering if it's possible to basically kind of "merge jobs" back together before ending, and if I could apply that elsewhere, with built-in job control in my scripts.

This is not a practical problem, just curious.


    while [ $i -lt 5 ]
        let i+=1
        sleep 1

    echo "Done"

1 Answer 1


Nope - Running processes can't do that sort of thing, because they're backgrounded by the shell and its the shell's job to say which process is foregrounded / backgrounded / suspended.

Try this - Remove the fg, and then run your shell script backgrounded (ie with a & on the end) When its finished, the output will be blurted to your console.

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.