Adding a Directory to the PATH
10 09 2009
- 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
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.
Obviously, you use this document at your own risk. I am not responsible for any damage or injury caused by your use of this document, or caused by errors and/or omissions in this document. If that’s not acceptable to you, you may not use this document. By using this document you are accepting this disclaimer.
Pre and Post Pathing
Linux determines the executable search path with the $PATH environment variable. To add directory /data/myscripts to the beginning of the $PATH environment variable, use the following:
To add that directory to the end of the path, use the following command:
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:
- 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.
- 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:
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:
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?
Adding to a Single User’s Path
To add a directory to the path of a single user, place the lines in that user’s .bash_profile file. Typically, .bash_profile already contains changes to the $PATH variable and also contains an export statement, so you can simply add the desired directory to the end or beginning of the existing statement that changes the $PATH variable. However, if .bash_profile doesn’t contain the path changing code, simply add the following two lines to the end of the .bash_profile file:
Adding to All Users’ Paths (except root)
You globally set a path in /etc/profile. That setting is global for all users except user root. Typical /etc/profile files extensively modify the $PATH variable, and then export that variable. What that means is you can modify the path by appending or prepending the desired directory(s) in existing statements modifying the path. Or, you can add your own path modification statements anywhere before the existing export statement. In the very unlikely event that there are no path modification or export statements in /etc/profile, you can insert the following 2 lines of code at the bottom of /etc/profile:
Adding to the Path of User root
User root is a special case, at least on Mandrake systems. Unlike other users, root is not affected by the path settings in /etc/profile. The reason is simple enough. User root’s path is set from scratch by its .bash_profile script. In order to add to the path of user root, modify its .bash_profile.
A fundimental administration task is adding directories to the execution paths of one or more users. The basic code to do so is:
Place that code, or whatever part of that code isn’t already incorporated, in one of the following places:
|User Class||Script to modify|
|All users except root||/etc/profile|