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

On osx/linux I want to be able to run a command/script on the terminal from anywhere which links to a program.

ie I want to be able to run:

alloy

that runs:

/usr/local/share/npm/lib/node_modules/alloy/bin/alloy

I'm guessing adding to .bashrc is the best way? I've tried running:

export PATH="$PATH:/usr/local/share/npm/lib/node_modules/alloy/bin"

and also:

export PATH="$PATH:/usr/local/share/npm/lib/node_modules/alloy/bin/alloy"

Then I started a new terminal window but the alloy command doesnt work. Am I missing something?

share|improve this question
    
Check that $PATH is actually set with echo $PATH. Does running /usr/local/share/npm/lib/node_modules/alloy/bin/alloy work? –  mgorven Oct 26 '12 at 23:20
    
Adding export PATH="$PATH:/usr/local/share/npm/lib/node_modules/alloy/bin" is correct. Try loggin off and log on again. –  Alexander Janssen Oct 26 '12 at 23:23
    
Are you users actually running bash as their shell? Setting it up for bash will not help those who fun csh, tcsh, zsh, plain old sh or anything else. –  Hennes Oct 27 '12 at 0:17
1  
.bashrc isn't the right place, it should be .bash_profile or .profile, since setting the PATH this way isn't idempotent, and .bashrc is executed for subshells. –  stew Oct 27 '12 at 2:20
    
@stew Ups, I haven't noticed your comment and answered exactly the same. The credit goes to you :) –  kubanczyk Oct 27 '12 at 9:18

2 Answers 2

up vote 2 down vote accepted

The best place to add to PATH is not the ~/.bashrc file, but the ~/.bash_profile if it exists. If .bash_profile does not exist, the best place is ~/.profile. The ~ part is automatically expanded to be your home directory (/home/you).

When you login and execute bash as the "login shell", it reads .bash_profile. If it is not found, it reads .profile. The .bashrc is NOT read in this case (unless it is explicitly called from inside .bash_profile). I rarely put anything in .bashrc; one thing that supposedly should go there are aliases if you'd like to use some.

To stay compatible with some old logon shells (like ksh88), which also read .profile, you could alternatively use a separate export line there:

PATH="$PATH:/usr/local/share/npm/lib/node_modules/alloy/bin"
export PATH

Your syntax is correct for .bash_profile:

export PATH="$PATH:/usr/local/share/npm/lib/node_modules/alloy/bin"

You can test it immediately, just enter one of above into your actual bash command line, and then try to see if it is recognized:

echo "$PATH"
which alloy
alloy

Now, as a test of your .bash_profile or .profile, login on a new session (not open just another terminal window, but really use your login and password).

To enable the same setting for all system users, change /etc/profile or /etc/profile.d, depending on your Linux flavor. This is a system-wide profile, read on each login before the ~/.bash_profile or ~/.profile.

share|improve this answer
    
Thanks! Adding it to ~/.profile and restarting terminal worked! –  Joe Oct 27 '12 at 9:13

One simply solution to this that doesn't require manipulating the path is to simply create a symlink to the application in the /usr/local/bin or /usr/local/sbin folders, since those folders are already in your path. I often use this for programs that install to /opt, /usr/local or other places which are not in the path by default.

ln -s /usr/local/share/npm/lib/node_modules/alloy/bin/alloy /usr/local/bin/alloy

Of course this might not always work if the program requires that it be run from a certain working directory, if a program had such a requirement, it would also be not work if you changed the PATH. I occasionally see this for badly design scripts that assume they are being called from a certain path.

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.