Skip to content

Instantly share code, notes, and snippets.

@kanonji
Last active August 15, 2016 11:26
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 kanonji/c275e167bb1e13876d27baa6933580e2 to your computer and use it in GitHub Desktop.
Save kanonji/c275e167bb1e13876d27baa6933580e2 to your computer and use it in GitHub Desktop.
Test that command substitution invokes a subshell.
#!/bin/sh
echo "\$ echo \$(pwd)"
echo $(pwd) #Current directory not changed.
echo "\$ \$(cd ..)"
$(cd ..)
echo "\$ echo \$(pwd)"
echo $(pwd) #Current directory not changed.
echo "\$ \`cd ..\`"
`cd ..`
echo "\$ echo \$(pwd)"
echo $(pwd) #Current directory not changed.
echo "\$ echo \$(cd ..; pwd)"
echo $(cd ..; pwd)
echo "\$ echo \$(pwd)"
echo $(pwd) #Current directory not changed.
echo "* * *"
echo "\$ cd .."
cd ..
echo "\$ echo \$(pwd)"
echo $(pwd) #Current directory changed.
@kanonji
Copy link
Author

kanonji commented Aug 15, 2016

Command substitution invokes a subshell.
http://tldp.org/LDP/abs/html/commandsub.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment