Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a Linux machine and I have a script called load_info and the script is located in /var

I would like to be able to run the load_info script from another directory without having to prefix the command with "./"

For example if I was in /root I want to be able to type load_info (without ./) and have this execute the /var/load_info script.

So how do I make it so that if I cd /root, I can type load_info and have it run the /var/load_info script.

share|improve this question

4 Answers 4

up vote 24 down vote accepted

Your shell will search the paths listed in the $PATH environment variable for the typed command if it's not fully qualified (e. g. vi instead of /usr/bin/vi). You can easily add another path to your $PATH variable by appending a line in your ~/.profile or ~/.bashrc:

export PATH="$PATH:/path/to/your/scripts"

As kind of best practice you should also save your script under /usr/local/bin or /usr/local/sbin, see hier(7).

share|improve this answer

You need to have the location of the script in an environment variable called PATH.

Environment variables are like normal variables you would use in your shell, except that they are persistent throughout your logged in session. Furthermore, if you set them to be loaded into your environment whenever you log in, they will always be set and available all the time.

The PATH variable helps the shell find the commands you enter. Its value is a list of directories that the shell searches every time you enter a command. The directory names are separated by colons.

If you are using the bash shell (the most common shell used in Linux), then you can see what directories are in the PATH variable by typing echo $PATH at the command prompt.

Let's just say that the load_info script is in a directory called /var. You can add this additional directory to the PATH variable by typing:

PATH=$PATH:"/var"

To make this a permanent change to the PATH variable, you can add this to three different files depending on what effect you want.

  • If you add it to the /etc/profile file, you will make the directory available to all users who log into your system.

  • You can add the PATH modification to your own .bash_profile file in your home directory, and this will only apply to you when you log in.

  • You could also add the PATH modification to the .bashrc file file in your home directory, which would have a similar effect to adding it to .bash_profile, except that the .bashrc file is loaded every time you launch a new shell.

It is also worth mentioning that /var is probably not the best place for a script anyway. /usr/local/bin would be better. Check out the heir(7) man page with man 7 heir for more information on what files should go where.

share|improve this answer
    
-1: adding /var is not the solution - the script shouldn't be there. –  symcbean Oct 31 '11 at 15:11
    
Fixed. You can +1 now. –  GarnerCX Nov 1 '11 at 12:41

There are some ways to do this: (running as root)

  1. Make a symlink to the /usr/bin

    # ln -s /var/load_info /usr/bin/load_info
    
  2. Add it to the global PATH environment variable:

    # echo "export PATH=$PATH:/var" >> /etc/profile
    
share|improve this answer
    
The PATH variable should never point to individual binaries (or scripts), only to the directories they're contained within. –  Vatine Oct 31 '11 at 16:45
    
If you make a symbolic link to the /usr/bin directory, that will most likely be in the PATH anyway. Even if it isn't you should add /usr/bin to the PATH, not /var. –  GarnerCX Nov 1 '11 at 4:53
    
I mean they are separate ways. –  quanta Nov 1 '11 at 4:59

In OS X (built on Unix) you could do it this way:

  1. pico .profile

  2. Add this line: -alias scriptname="~/Path-to-script/./scriptname" -Exit pico and save.

  3. . .profile

As seen here: http://rmcaps.tumblr.com/post/11635645690/help-whats-my-ip-updated

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.