0

I want to do a very simple install process for my app framework. I've got everything working, but I want to set two environment variables. I want to update the PATH and also set a HOME variable.

I know I can't set a var on the parent process, so how can I do this? I don't want to require the user to execute source foo, I want them to be able to just run foo install and the deed is done.

Surely, there is SOME way?

EDIT: Sorry - I don't mean the actual HOME, I mean a MY_FRAMEWORK_HOME variable!

  • . Somefile or source somefile are the normal ways. If you can give them a function, that's another way. – Jeff Schaller Jan 28 '16 at 23:33
  • HOME=/my/home PATH=/my/path my.install – mikeserv Jan 28 '16 at 23:36
  • Or suggest lines to them that they can edit into their shell profile – Jeff Schaller Jan 29 '16 at 0:00
2

The $HOME variable isn't something you should mess with, generally; it's user-dependent. You don't want the user to suddenly have a different home directory! (And if you do, you should edit his home directory that is set in /etc/passwd—which of course requires root permissions.)

The $PATH is another matter and should be set in his ~/.bashrc or ~/.bash_profile. There are various arguments as to which you should use, which I won't get into here.

Let's say you want to append /var/myapp/bin to the user's path. You can do this by adding the line:

export PATH="$PATH:/var/myapp/bin"

to the user's ~/.bashrc. A good method of doing this while avoiding adding the line if it's already there is discussed on the vi stackexchange:

ex -sc '$a
export PATH="$PATH:/var/myapp/bin"
.
$-,$!uniq
x' ~/.bashrc

Be warned that if you are adding more than one line this way, the same safety check to avoid adding them if they are already there won't work. You'll need different logic for that.


You might also consider a different course of action: Add symlinks to your command binaries into /usr/local/bin, which is typically already included in the $PATH.

So let's say, as in my example above, that you want to add /var/myapp/bin to the user's PATH. The reason you might want to do this in the first place is because you have some commands (shell scripts or binaries, doesn't matter) in /var/myapp/bin that you want the user to be able to execute from the command line. Let's say these commands are myapp_command and myapp_othercommand.

You could make these available in the PATH by creating symlinks like so:

ln -s /var/myapp/bin/myapp_command /usr/local/bin/myapp_command
ln -s /var/myapp/bin/myapp_othercommand /usr/local/bin/myapp_othercommand

The first argument to ln -s is the TARGET, the second argument is the LINK name you want to create to point to the target. (For more, see man ln.)

  • Excellent! As a side note, I'm not messing with the actual HOME var, I want MY_HOME. Can you describe a bit more how adding the symlinks from a shell script would look? – mtyson Jan 29 '16 at 1:46
  • @mtyson, I added some more details. :) – Wildcard Jan 29 '16 at 2:05
-1

Just export variables:

export NAME1="value1"
export NAME2="value2"
./my.install

Edit: Doing this is possible from following shells (I know):

  • DASH
  • BASH
  • KSH
  • ZSH

Edit 2: Move my.install to my.install.real, and create a new file called my.install:

export HOME='SAMPLE VALUE'
export PATH='SAMPLE VALUE'
./my.install.real
unset HOME
unset PATH
  • From the question you are answering: "I know I can't set a var on the parent process, so how can I do this?" The point is to set a permanent environment variable. For that you need to edit the profile. – Wildcard Jan 29 '16 at 2:06

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.