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.
.profile
can contain anything. There may be a line that setsPATH
or not, there may be complicated code to setPATH
. You cannot safely modify such a line. You could append some code that modifiesPATH
(again)..profile
is specific for a user. Do you want to modifyPATH
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
– Bodo Jan 22 '19 at 09:23bin
directories like/usr/bin
or/usr/local/bin
?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