Question: Quite often, the script I want to execute is not located in my current working directory and I don't really want to leave it.

Is it a good practice to run scripts (BASH, Perl etc.) from another directory? Will they usually find all the stuff they need to run properly?

If so, what is the best way to run a "distant" script? Is it

```
. /path/to/script

```

or

```
sh /path/to/script

```

and how to use `sudo` in such cases? This, for example, doesn't work:

```
sudo . /path/to/script

```

Answer: 
sh /path/to/script will spawn a new shell and run she script independent of your current shell. The `source` (.) command will call all the commands in the script in the current shell. If the script happens to call `exit` for example, then you'll lose the current shell. Because of this it is usually safer to call scripts in a separate shell with sh or to execute them as binaries using either the full (starting with `/`) or relative path (`./`). If called as binaries, they will be executed with the specified interpreter (`#!/bin/bash`, for example).

As for knowing whether or not a script will find the files it needs, there is no good answer, other than looking at the script to see what it does. As an option, you could always go to the script's folder in a sub-process without leaving your current folder:

```
(cd /wherever/ ; sh script.sh)

```
I usually do like you say

```
sh /path/to/script

```

And to run it as root/superuser

```
sudo sh /path/to/script

```

Your current directory only matters if the scripts assumes you are in the same folder as it. I would assume most scripts don't do this and you are save to run it like above.