$(…) captures the output of commands into a variable, but it does so by spawning a subshell—a child process separate from your main shell:
Assignments made inside the subshell do not affect variables in the parent shell.
Overusing complex command substitutions inside tight loops can degrade script performance. Measure and optimize if needed.

1. Subshell Syntax
Wrap commands in parentheses to run them in a subshell:
2. Command Substitution with a Subshell
Combine a subshell with$(…) to capture its output separately from the parent shell:
3. Command Layout in Subshells
Within parentheses, you can:- Put commands on separate lines
- Separate with semicolons (
;) - Pipe between them (
|) - Chain with
&&or||

4. Common Subshell Scenarios
4.1 One-Liners to Change Directory Temporarily
Run commands in a different folder without affecting your current directory:
4.2 Jenkins Pipeline Steps
In Jenkins Pipelines, eachsh step executes in its own subshell. This isolation explains differences when scripts run in CI/CD environments.
5. Verifying Process IDs
Use$$ and $BASHPID to compare parent and subshell PIDs: