0

In Bash 5.0.3, I'm running the following command and I'm curious why the output spawns an additional shell after the first one completes?

( ( ( sleep 1 ) && echo "done sleeping for one second" & ) || \
( ( sleep 0.5 ) && echo "done sleeping for half second" & ) ) && echo $?

By spawn an additional shell, I mean the terminal itself returns the prompt to the user and interrupts it once again:

user@system $ ( ( ( sleep 1 ) && echo "done sleeping for one second" & ) || ( ( sleep 0.5 ) && echo "done sleeping for half second" & ) ) && echo $?
0
user@system $ done sleeping for one second
^C
user@system $ 
  • 1
    `I mean the terminal itself returns the prompt to the user and interrupts it once again` - I do not understand that. There is no "additional shell" - which part of the output is exactly not clear to you? Which part exactly of the output you interpret as "additional shell"? Well, the command spawns a background process, that waits 1 second and then prints something, so the background process is going to finish a 1 second after the foreground `echo $?` finishes. – KamilCuk Feb 29 '20 at 19:45
  • 2
    This is an entirely cosmetic effect explained in detail [here](https://stackoverflow.com/a/28282164/1899640). You'll find that `ls` still works exactly as expected, even though the screen is slightly garbled from the asynchronous output – that other guy Feb 29 '20 at 20:20
  • 1
    you kicked off a process in the background ... said (background) process generates output ... where do you think that (background process) output is going to show up? – markp-fuso Feb 29 '20 at 20:30
  • 1
    I think no reference provided fully explains what is happening. A crucial point to understand is that running a subshell in background with `( command & )` immediately returns 0, so `echo $?` outputs immediately. – Quasímodo Feb 29 '20 at 20:37

0 Answers0