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

Related Post

Go to the Previous Directory in Vim I use Vim. Vim is the most productive text editor if you know some vim commands When I open a file and I am done with it. I often want to go...
Export Data From Firebird Database to CSV File Background Some software applications use a database called Firebird. This is an open source database. If the software application is compiled you ca...
Installing Ruby on Ubuntu Installing Ruby on Ubuntu As seen on RVM... Go to the terminal in Ubuntu. Add the public key from RVM $ gpg --keyserver hkp://keys.gnupg.n...
Installing Hipchat on Ubuntu Tested on: Ubuntu 12.04 LTS Thinkpad T400, 2GB RAM Intel Core 2 Duo CPU P8600, 32bit Sources: Official hipchat linux instructions Ask U...

Leave a Reply