Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
Be aware that . /path/to/script sources the script! You don't need the period at all if you just want to run it. – gniourf_gniourf Nov 25 '12 at 8:35
up vote 6 down vote accepted

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

You can definitely do that (with the adjustments the others mentioned like sudo sh /pathto/ or ./ However, I do one of a few things to run them system wide to not worry about dirs and save me useless extra typing.

1) Symlink to /usr/bin

ln -s /home/username/Scripts/ /usr/bin/name

(be sure there is no overlapping name there, because you would obviously override it.) This also lets me keep them in my development folders so I can adjust as necessary.

2) Add the Scripts dir to your path (using .bash_profile - or whatever.profile you have on your shell)


3) Create Alias's in the .bash_profile in ~/.bash_profile add something like:

alias l="ls -l"

As you can tell, the syntax is just alias, digits you want to act as a command, the command. So typing "l" anywhere in the terminal would result in ls -l If you want sudo, just alias sl="sudo ls -l" to note to yourself l vs sl (as a useless example).

Either way, you can just type sudo nameofscript and be on your way. No need to mess with ./ or . or sh, etc. Just mark them as executable first :D

share|improve this answer
I would highly recommand option 2. – Bernhard Nov 25 '12 at 11:04
Why?, it's a best practice or just taste? – Sergio Sep 7 '14 at 4:42

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.

share|improve this answer
would not work if secure_path is set in /etc/sudoers file – l1zard Nov 24 '12 at 23:09

I usually keep my scripts in /usr/local/bin or /usr/local/sbin/ (if the script needs root privileges) where, according to the Filesystem Hierarchy Standard (FHS), they belong.

All you have to do is to make sure these two directories are added to your PATH. You can do this by editing your $HOME/.bashrc file and adding this line:

export PATH=$PATH:/usr/local/sbin:/usr/local/bin

If you want to be able to execute a script as root via sudo, you have to add these directories to the variable secure_path in your /etc/sudoers.

Defaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Editing this file is done by running visudo which ensures you don't have any mistakes.

share|improve this answer
Typo: you mean .bashrc instead of .bachrc. – gniourf_gniourf Nov 25 '12 at 14:35
yes of course. thanks man :) – l1zard Nov 26 '12 at 7:02

I'm not sure it works like this in linux, assuming it doesn't if no-ones suggested it. But instead of using ././ to go back directories. Can you use quotes to give it an absolute path? Maybe it doesn't give you access to the whole drive to even be able to do that actually come to think of it.

share|improve this answer

If you have scripts lying around that you need to run often, and they depend on their location for finding resources you can easily do this by just combining commands in an alias like this.

alias run-script="cd /home/user/path/to/script/ && bash"

This way you don't have to alter anything else to make it work.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .