I have found a behavior of shell I don't understand. When you executeecho foo > /tmp/bar & exit, the terminal is closed. But when you executeexit & echo foo > /tmp/bar, the terminal stays open and something like[1] 4001is printed on it.The output of the first version can be seen, too, if you log into a different account using su, because then, only the session of the user you logged in to using su is ended and your terminal stays open. It's something like:[2] 13777exit[1]+  Done                    exitWhy isn't the session ended if exit is the first command?Just in case you can't replicate this on your Unix system: I'm using Ubuntu 16.04.
End of session if exit command run in parallel to different command
bash;shell;command line;terminal
Your first command,echo foo > /tmp/bar & exitstarts a subshell in the background to run echo foo > /tmp/bar, and exits (the foreground shell). This closes your terminal, in the same way as simply typing exit. The background shell won't stay around very long at all, so you won't get a race; but if you do this with a longer-running command, depending on your shell and your options, you'll get different behaviours:sleep 60 & exitmight not exit but complain that you have running jobs instead.In both these cases, the shell does print a line of the form [1] 7149 but if your terminal closes you won't have time to see it.Your second command,exit & echo foo > /tmp/barstarts a subshell in the background to run exit, which exits (from the subshell), and runs echo foo > /tmp/bar. So the main shell doesn't exit and the terminal stays open, which allows you to see[2] 13777followed byexit(printed by the shell exiting) and then[1]+  Done                    exit(printed by the main shell when the background shell exits).