I want to add a directory to the $PATH environment variable through an install script. I have written a few programs, all present in one directory. They need a lot of other packages installed to run. So I also wrote an installation script to install all the dependencies. All of that is fine. However, what I finally need to do is to also add the directory to the search path, preferably directly through the installation script, and then do source ~/.profile as well from the script itself if possible.

So basically, I would like to write a script that basically adds a directory to the search path permanently if not previously present. I can do the "if not present part". I, however, did not find any resources on how to add the path safely to .profile through a script. Is it not advisable to do so?

I think I can try deleting the line containing the $PATH using sed and append the new one to the .profile file. I do not want to try that and risk messing something up unless I am absolutely sure.

  • 3
    There are several problems with your approach. .profile can contain anything. There may be a line that sets PATH or not, there may be complicated code to set PATH. You cannot safely modify such a line. You could append some code that modifies PATH (again). .profile is specific for a user. Do you want to modify PATH for every user? Then it would be better to modify /etc/profile. I think the installation concept might be wrong. Is it not possible to install the programs (or wrapper scripts) into one of the default bin directories like /usr/bin or /usr/local/bin? – Bodo Jan 22 '19 at 9:23
  • I want to preferably copy the whole directory to someplace specified by the user. If there is no possible way to do this safely, then I would try doing something like you suggested – Yuki.kuroshita Jan 22 '19 at 9:27
  • 1
    If Linux, consider also the /etc/profile.d/ directory – Jeff Schaller Jan 22 '19 at 10:56
  • 2
    Does my question need major improvements? Why was it downvoted? I want to request anyone downvoting the question to first tell me what changes need to be made so that the question can be improved. Just downvoting does not tell me anything. – Yuki.kuroshita Jan 22 '19 at 11:02
  • 2
    Standard practice is to let the user modify their own .profile when installing a package -- automatically editing it can cause damage and/or upset users. Downvotes might be from that. I'll answer more in your related meta question. – stevegt Jan 23 '19 at 21:29

As Bodo said, it could be too complicated to modify an existing PATH variable, so keep it simple. You could just add your code to the end of the file like:

echo -e "\n# path added by my personal installer" >> ~/.profile
echo "[ -d /path/to/my/directory ] && PATH=\"/path/to/my/directory:\$PATH\"" >> ~/.profile

AND output a message at the end of the installer like "Note: /path/to/my/directory was automatically added to PATH variable in ~/.profile"

Or you leave it to the installing user to add the path and output a message like this at the end of your installation script:

Please add the path to /path/to/my/directory to your ~/.profile or ~/.bashrc
Example snippet:
    if [ -d /path/to/my/directory ]; then
| improve this answer | |
  • Note, that .profile is only sourced on logon. So the user has to log off and log on again to get this settings active. – Ralf Jan 22 '19 at 11:44
  • Yes, but not if the file is sourced again. Yuki wanted to source ~/.profile from the script itself. – Freddy Jan 22 '19 at 11:48
  • But that would only source the file in the installation script. After that installation script exits, the sourced settings are gone. – Ralf Jan 22 '19 at 11:58
  • You are right, good point! – Freddy Jan 22 '19 at 12:20
  • I realize that sourcing from the script would probably be impossible so after implementing the solution above, I am going to show a message to run source ~/.profile after the installation and also include the instruction inside the README file just in case. That seems to be the only thing that I can do. – Yuki.kuroshita Jan 22 '19 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.