Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

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.

share|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

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

/path/to/script

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:

#!/bin/bash
ls

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

test@ubuntu:~:/tmp/test.sh
Desktop     Dropbox         Imagens  NetBeansProjects     Público     
Documentos  Modelos    R          Vídeos

In this situation, dirname is your ally:

#!/bin/bash
current=$(dirname $0)
ls $current

Running it from your home folder, it gives:

test@ubuntu:~:/tmp/test.sh
acroread_1000_1000     hiRF7yLSOD              pulse-2L9K88eMlGn7   
unity_support_test.0    clementine-art-jt5332.jpg

which is the content of my /tmp/ folder.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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