A subshell creates a separate instance of the command processor, or a subprocess of the parent shell. Although a subshell is started in its parent’s working directory, directory changes made within the subshell don’t carry over to its parent. This makes subshells ideal for running one-off commands in a different directory:
$ pwd /Users/jeffkreeftmeijer/rust/conway/ $ (cd www && npm install) [...] $ pwd /Users/jeffkreeftmeijer/rust/conway/
In this example, the command is run in the www
directory [1], but after it completes, the main shell is in the same place.
1. npm has a
--prefix
flag that allows running the command in another directory. Instead of using a subshell you could also run npm install --prefix www
and get the same result.