Contents:
Disclaimer
Pre and Post Pathing
Adding to a Single User's Path
Adding to All Users' Paths (except root)
Adding to the Path of User root
Summary
Executive Summary
Adding a directory to the path of a user or all users would seem trivial, but in fact it isn't.
The best place to add a directory to the path of a single user is to modify that user's
.bash_profile file. To add it to all users except user root, add it to /etc/profile. To also
add it to the path of user root, add it to root's .bash_profile file.
PATH=/data/myscripts:$PATH
To add that directory to the end of the path, use the following command:
PATH=$PATH:/data/myscripts
But the preceding are not sufficient because when you set an environment variable inside a
script, that change is effective only within the script. There are only two ways around this
limitation:
1. If, within the script, you export the environment variable it is effective within any
programs called by the script. Note that it is not effective within the program that called
the script.
2. If the program that calls the script does so by inclusion instead of calling, any
environment changes in the script are effective within the calling program. Such
inclusion can be done with the dot command or the source command. Examples:
. $HOME/myscript.sh
source $HOME/myscript.sh
Inclusion basically incorporates the "called" script in the "calling" script. It's like a #include in
C. So it's effective inside the "calling" script or program. But of course, it's not effective in any
programs or scripts called by the calling program. To make it effective all the way down the call
chain, you must follow the setting of the environment variable with an export command.
As an example, the bash shell program incorporates the contents of file .bash_profile by
inclusion. So putting the following 2 lines in .bash_profile:
PATH=$PATH:/data/myscripts
export PATH
effectively puts those 2 lines of code in the bash program. So within bash the $PATH variable
includes $HOME/myscript.sh, and because of the export statement, any programs called by
bash have the altered $PATH variable. And because any programs you run from a bash prompt
are called by bash, the new path is in force for anything you run from the bash prompt.
The bottom line is that to add a new directory to the path, you must append or prepend the
directory to the $PATH environment variable within a script included in the shell, and you must
export the $PATH environnment variable. The only remaining question is: In which script do you
place those two lines of code?
PATH=$PATH:/data/myscripts
export PATH
Summary
A fundamental administration task is adding directories to the execution paths of one or more
users. The basic code to do so is:
PATH=$PATH:/data/myscripts
export PATH
Place that code, or whatever part of that code isn't already incorporated, in one of the following
places: