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


sh /path/to/script

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

sudo . /path/to/script

You can call your script using only the whole path, without the dot .:


sudo also works fine:

sudo /path/to/script

Will they usually find all the stuff they need to run properly?

Do you mean like, "will my script find the files which are in the same folder?" That depends of your code. For example, if you have the script /tmp/test.sh:


If you invoke it from your home folder, it will run ls in your home:

Desktop     Dropbox         Imagens  NetBeansProjects     Público     
Documentos  Modelos    R          Vídeos

In this situation, dirname is your ally:

current=$(dirname $0)
ls $current

Running it from your home folder, it gives:

acroread_1000_1000     hiRF7yLSOD              pulse-2L9K88eMlGn7   
unity_support_test.0    clementine-art-jt5332.jpg

which is the content of my /tmp/ folder.

| improve this answer | |

Common *nix practice for user scripts is to put them in your bin directory. That way, the script will be searched for by default when you type the name from the command line.

Your bin (binary) directory should be in your home folder so the full path would be /home/yourusername/bin. (Obviously change yourusername to your login name.)

The bin directory should have the following permissions if you run ls -lh from the terminal: drwxr-xr-x

By default when you make a directory with the terminal you have the following permissions set: drwxrwxr-x

The only thing you need to change is the group permission, but for I will walk you through each step from the terminal (assuming you don't already have a bin):

  • Open a terminal ([Ctrl] + [Alt] + t)

  • Create the binary directory: mkdir bin

  • Now either use the GUI or CLI to copy your scripts to /bin.

  • Now we have to change some permissions so that everything is set up correctly:

  • chmod -R 755 bin

  • This sets the owner (you) to read, write and execute, groups and others to read and execute the scripts. The -R means recursive or set the permissions for all the files in the directory as well.

For more help on file permissions see this document.

Now, you can just type myscript to execute your scripts - you do not have to be in the directory. You don't need the . / or anything!

For sudo it's a little different as you will have to define the full path to the script file.

| improve this answer | |
  • Is it possible for new scripts added to or created in the script directory to pick up the executable permission automatically, perhaps with a sticky bit? – Desmond Hume Nov 25 '12 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.