Follow this to execute a script in a different directory in Linux.

I had a script in ~/Documents/scripts/ and wanted to run it inside a different directory ~/Documents/images/awesome-images/.

The Subshell

You can use parentheses to create a subshell. Once the command is completed, the subshell will close, such as…

$ (cd ~/Documents/images/awesome-images/ && ~/Documents/scripts/

This is how it works:

  • Use parentheses to create a subshell
  • cd into ~/Documents/images/awesome-images/
  • If this command is successful then
  • Execute the script located in ~/Documents/scripts/
  • When this command is executed. Close the subshell