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?
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:
Your syntax is correct for .bash_profile:
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:
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.
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.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.