views:

29

answers:

3

I want to make the following kind of reference:

"ls" command, for example, is universally available in most *nix environments. User can type in from anywhere to execute the scripts.

So, I write script "x". I want to make sure that from wherever the user type in x, the actual script "x" is referenced.

Thus, if I have script "x" stored in home/user/Desktop directory, I should not have to reference the script as follow:

home/user/Desktop/x

I should be able to do:

x

Thanks!

+1  A: 

You want to add the directory to your PATH. E.g.

PATH="$PATH:/home/user/someDirectory"

You can add this line to .bash_profile to do it on startup. However, you probably shouldn't add Desktop to the path because some browsers download to there by default (though it shouldn't be executable by default).

Matthew Flaschen
A: 

You can also put your script in an existing directory that's already in your path such as /usr/local/bin or create a symlink there to your script's location.

cp /home/user/Desktop/x /usr/local/bin

or

mv /home/user/Desktop/x /usr/local/bin

or

ln -s /home/user/Desktop/x /usr/local/bin
Dennis Williamson
A: 

Don't mean to be obnoxiously repetitive, but this is my first time answering a question, I can't reply to someone's already-good answer, and I think they are missing some important bits.

First, if you want to make sure everyone can access the script, you'll need to be sure everyone has execute permissions:

chmod a+x /path/to/script.sh

You'll also want to make sure it's in somewhere $PATH references (as the other answers mentioned):

echo $PATH # place the script in one of these directories

I would personally prefer /usr/local/bin, since that's considered the place for custom global scripts. Something the other answers didn't mention is that, if you do want to use a directory besides one in $PATH (say, /opt/myscriptfolder/) you'll want to add another PATH entry at the end of /etc/profile:

PATH="$PATH:/opt/myscriptfolder/"

By putting this in the end of /etc/profile, all users will receive this modified PATH variable on their next login.

javaJake