0

How can I best write a script to append to the user's PATH variable permanently, regardless of the type of shell they may be using?

It is easy to simply append a line to an existing startup file a line such as this (for bash):

echo "export PATH=$PATH:~/newpath" >>~/.bash_profile

The first problem in doing this for different shell types is that the name of the startup file may be different (and even for the bash shell it may have one of several different names). The second problem is that the new startup line(s) differ by shell type. The third problem is that I need the shell type of the parent to my script process, not the script process itself which will use a shebang to ensure bash.

The third problem seems easily solved with a command like this:

ps -p $PPID -oargs=

within my script invoked from a shell that returns the hyphen prefixed name of the parent shell, even if the script itself is always running in bash.

The first problem can be solved if I limit the applicability to just a few shell types, for which I believe bash, csh, tcsh, and zsh would be sufficient, and I could have a remaining case which just gives a helpful message for any other kind of shell not supported. In the specific case of bash on the OS of interest (Mac OS X) I need to check for existing files .bash_profile, .bash_login, and .profile, in that order, and edit the first one that exists. It should be just as easy or easier for the other listed shells.

The second problem can be solved by just looking at how the PATH variable is set for each different type of shell. I don't think this will be hard either.

An entirely different approach might be to simply look for ALL of the startup files for the supported shells and modify the first startup file for each shell type found.

A significant refinement would be to grep each startup file being modified to ensure it hasn't already been modified, but there may be the possibility of an incomplete or botched previous modification.

Now to those who say I should just let the user do this, the situation is that I am porting a linux program which runs in a shell to the Mac OS X Terminal app environment. In this case most users will be unfamiliar both with how to set shell variables, and how to use text editors to edit startup files. So I think it is best if I provide a canned solution like this. There is also a history among users of this program on Solaris, where it originated, to use csh or a derivative of csh instead of bash, so I feel I need to be prepared for that.

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • Why are you modifying other peoples start up profiles? Seems to me that you are infringing on there personal set up and may end up breaking things or have unintended consequences. For example they may wish to only use your application for experimentation/evaluation at this stage – Ed Heal Mar 23 '16 at 03:57
  • The changes you describe are often coverred as manual instruction in README.TXT or INSTALL.TXT documents. Good luck. – shellter Mar 23 '16 at 04:05
  • Ask on https://apple.stackexchange.com – trojanfoe Mar 23 '16 at 09:12
  • Have you read http://stackoverflow.com/questions/135688/setting-environment-variables-in-os-x – fork2execve Mar 30 '16 at 21:27

0 Answers0