How to execute a script in a different directory than the current one?

Solution 1:

The leading dot in your command means "relative to the current directory". Remove it and it'll refer to "the file someScript in the directory /home/user/scripts:

/home/user/scripts/someScript

If you get "Permission denied", it's either because you do not have sufficient permissions to access the file in the directory of other users or because the file is not executable. To make it executable, run:

chmod +x /home/user/scripts/someScript

Solution 2:

If your script needs to access resources in the same folder that it is being run from, and you have it specified as relative paths, then your script will break.

I always add a cd $(dirname $0) to the head of my script so the folder containing the script will be the root folder.

Solution 3:

Remove the .

If you make the scrip executable with chmod 755 <nameofscript> to run it you only need to type the path to the script.

When you see ./script being used it telling the shell that the script is located on the same directory you are executing it. To use full path you type sh /home/user/scripts/someScript.

Solution 4:

I tried many ways and that's working well with me:

(cd ~/user/scripts/; ./someScript)

I get some help from this answer.