7

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.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • 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 09: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 09:27
  • If the programs should be used by this one user only you can try to add the directory to PATH in .profile. I suggest to append some code at the end of .profile that checks if the directory is already in $PATH and appends or prepends it if it's not already present. See also https://unix.stackexchange.com/q/26047/330217 – Bodo Jan 22 '19 at 09:37
  • 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
  • Thank you @stevegt. As I asked in the question, I wanted to know whether it is safe to do this. I think the safest way to go about achieving a similar result would be to make symbolic links to all the files in the directory I want added to the path inside a directory already in the path. – Yuki.kuroshita Jan 24 '19 at 00:29
  • It may be worth reflecting on why the standard process for installing software isn't appropriate. I would like to suggest something vaguely similar to Jeff Schaller: if /etc/profile.d exists and is a directory, you should be able to add a file there for your package to add the code to check if the directory in question is in the user's path and add it if it's not. I for one very much do not want a package I installed updating my .zshrc file. But then, I don't keep my .zshrc in the normal place... for that reason. – Ed Grimm Feb 03 '19 at 04:15

1 Answers1

5

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
        PATH="/path/to/my/directory:$PATH"
    fi
Freddy
  • 25,565
  • 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