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: 
Not sure why no one has suggested this one, but it's super easy! I've Googled a few times and couldn't find this exact answer I'm giving so I'd thought I'd share. IMO, this but the best solution, also the easiest one, for me anyway, however others may feel and do things differently. 

```
# Place this somewhere in your .bashrc/.bash_profile/etc and edit as you see fit

YOURCOMMAND () {
  cd /path/to/directory/containing/your/script/ && ./YOURSCRIPT
}

```

First the 'cd' command telling it the directory of the scripts location. Then '&&' so that you can tie it after excuting the next command. Finally open your script just as you would would execute it within terminal! Saved your in your BASH file and takes 5 seconds to setup.

Hope this helped someone.
Ancient question, but a timeless one.

The solution I've consistently seen is to have a `$HOME/bin` directory and put it first in `$PATH` (via `~/.bashrc` if it isn't already there; on some systems `~/bin` is first in `$PATH` by default). Dropping scripts in there for execution or symlinks to scripts/executables elsewhere is the simple way to deal with path issues that shouldn't affect the system or other users.

If a script requires additional resources that can be found *relative to its own location* (not uncommon) then the envvar `$BASH_SOURCE` is used. `$BASH_SOURCE` always contains the absolute path to the currently running script itself, regardless the value of `$PWD`.

Consider the following:

```
ceverett@burrito:~$ echo $PATH
/home/ceverett/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

```

So we can see that `$HOME/bin` is first in `$PATH`, so whatever I put in `~/bin` will run. I have a demonstration script called `~/bin/findme`:

```
#!/bin/bash

echo "Running from $BASH_SOURCE"

```

This can be used to get the absolute path to the running script's location.

```
ceverett@burrito:~$ findme
Running from /home/ceverett/bin/findme
ceverett@burrito:~$ cd foo
ceverett@burrito:~/foo$ findme
Running from /home/ceverett/bin/findme
ceverett@burrito:~/foo$ cd /
ceverett@burrito:/$ findme
Running from /home/ceverett/bin/findme

```