Introduction
Subshells let you run a group of commands together in a separate shell. This keeps your main shell safe from changes like directory moves or variable updates.
( command1; command2; command3 )
(cd /tmp; ls)
output=$( (echo Hello; echo World) )
(VAR=5; echo $VAR)#!/bin/bash # Show current directory pwd # Run commands in a subshell ( cd /tmp echo "Inside subshell:" pwd ) # Show current directory again pwd
(commands)?var=hello (echo $var; var=world; echo $var) echo $var
(cd /tmp; ls; cd /home) echo $PWD
echo $PWD not show /home?Pass the test and this pattern moves unknown