I have a ./scr script.abc@~ $ cat scr#!/bin/bashpsecho '-------'echo $(ps)abc@~ $ My goal is to find out how are the subprocesses created.As far as I know, the $(...) part should create a subshell, and therefore a new process. So the number of processes in the second call of ps should be larger.That's exactly how it is if I source the script in the current shell:abc@~ $ . scr  PID TTY           TIME CMD 1659 ttys000    0:00.17 -bash-------  PID TTY           TIME CMD 1659 ttys000    0:00.17 -bash 1785 ttys000    0:00.00 -bashabc@~ $ However, when launching in an interpreting shell, the number of processes doesn't differ:abc@~ $ ./scr  PID TTY           TIME CMD 1659 ttys000    0:00.17 -bash 1790 ttys000    0:00.00 /bin/bash ./scr-------  PID TTY           TIME CMD 1659 ttys000    0:00.17 -bash 1790 ttys000    0:00.00 /bin/bash ./scrabc@~ $ Why is it so?Similarly, why does ps give the same output as (ps)?abc@~ $ ps  PID TTY           TIME CMD 1659 ttys000    0:00.18 -bashabc@~ $ (ps)  PID TTY           TIME CMD 1659 ttys000    0:00.18 -bashabc@~ $ An interesting thing is that prepending the ps command with any other command forces it to produce the expected new process (produces the expected process in the script at the top, in ./scr, as well).abc@~ $ (echo 1; ps)1  PID TTY           TIME CMD 1659 ttys000    0:00.20 -bash 1823 ttys000    0:00.00 -bashabc@~ $ Is (ps) being somehow optimised by the shell? And why is it not, when sourced?A side note: the system is actually a macOS, I don't expect it to behave differently in that case, though.EDIT:As in this answer, the subshell seems to be a subject to optimisation, and therefore is not being run in a separate, newly initiated shell, because apparently it's not needed.Why is it needed when running in current shell, then (. scr)?
Subshell does not create a separate process
process;subshell