abhigenie92@ubuntu:~$ (sleep 2; echo $BASH_SUBSHELL ;sleep 2)
abhigenie92@ubuntu:~$ (sleep 2; echo $BASH_SUBSHELL ;sleep 2)&
[1] 3297
abhigenie92@ubuntu:~$ 1

[1]+  Done                    ( sleep 2; echo $BASH_SUBSHELL; sleep 2 )

What happens when launch it as a background process, why don't I get the prompt back and I have to press Enter? In both cases it launches a child shell.

  • I don't understand your question, exactly? Do you mean how bash doesn't report that the job is done until the next prompt is displayed (whereas, for example, zsh does)? – Celada Mar 7 '16 at 20:05

You got the prompt back (see the fifth line of your code block) — and then you got the output.  You were still "at the shell prompt" when you typed Enter the third time.

In theory, the shell could be programmed to reissue the prompt when a background process terminates.  But

  1. That wouldn't really be very helpful.  Your issue is that, when the background job did the echo 1 and the 1 appeared on your terminal, your cursor went back to the left edge of the screen, and it became non-obvious that you were still "at the shell prompt".  And the foreground shell doesn't know when a background task writes to the screen.
  2. Historically, the shell doesn't know when you're typing a command; it knows only when you type Enter.  (That may still be the case in some contexts.)  So the shell wouldn't know if you were in the middle of typing a third command

    (sleep 3; echo someth

    and it would be really confusing if you got a new shell prompt when you were in the middle of typing a command.

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.