Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jeffkreeftmeijer
Last active February 2, 2021 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffkreeftmeijer/24ac2dc40480fbd3d3c6b227ce051521 to your computer and use it in GitHub Desktop.
Save jeffkreeftmeijer/24ac2dc40480fbd3d3c6b227ce051521 to your computer and use it in GitHub Desktop.

Subshells

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment