Linux Shell Scripting Cookbook(Third Edition)
上QQ阅读APP看书,第一时间看更新

Spawning a separate process with subshell

Subshells are separate processes. A subshell is defined using the ( ) operators:

  • The pwd command prints the path of the working directory
  • The cd command changes the current directory to the given directory path:
        $> pwd 
        / 
        $> (cd /bin; ls) 
        awk bash cat... 
        $> pwd 
        /

When commands are executed in a subshell, none of the changes occur in the current shell; changes are restricted to the subshell. For example, when the current directory in a subshell is changed using the cd command, the directory change is not reflected in the main shell environment.