Yes, what you see is optimization. Technically, a design (β¦) always launches a subshell by definition. In most cases, the subshell works in a separate subprocess. This ensures that everything that is done in the subshell remains in the subshell. If bash can guarantee this isolation property, he can use any implementation technique that he likes.
In the fragment ( ps -o pid,ppid,cmd ) it is obvious that nothing can influence the parent shell, therefore there is optimization in bash, which makes it not fork as a separate process for the subshell. The fragment ( echo hello ; ps -o pid,ppid,cmd ) too complex for the optimizer to recognize that no subshell is required.
If you are experimenting with ksh , you will notice that its optimizer is more aggressive. For example, it does not support a subprocess for ( echo hello ; ps -o pid,ppid,cmd ) .
source share