Execute a Script in a Different Directory in Linux

As seen here.

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

The Subshell

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

$ (cd ~/Documents/images/awesome-images/ && ~/Documents/scripts/awesome-script.sh)

The && allows you to execute many commands in one line.

Execute a Script in a Different Directory in Linux

$ (cd ~/Documents/images/awesome-images/ && ~/Documents/scripts/awesome-script.sh)

This is how it works:

  1. Use parenthesis to create a subshell
  2. cd into ~/Documents/images/awesome-images/
  3. If this command is successful then
  4. Execute the script located in ~/Documents/scripts/awesome-script.sh
  5. When this command is executed. Close the subshell

Leave a Reply