3

Assume I want to export a path, let's say FOOBAR and make the changes not only effective immediately and everywhere but also permanent. My goal is to have a list of commands that can be entered manually in an active shell session as well as executed from within a shell script (which in turn might be executed via the shell or a GUI file manager etc.).

Old processes don't need to be aware of any changes.

Here's what I want to happen to FOOBAR:

  • Add a new value without overwriting previous values.
  • Make the change accessible system-wide (for any program or shell session, no matter how it is launched) within the current user context.
  • Make the change effective immediately (i.e. no logout required etc.)

My current approach is exporting the value as a set-command to both .profile and .bashrc:

echo "export FOOBAR=$FOOBAR:$HOME/example/" >> $HOME/.profile;
echo "export FOOBAR=$FOOBAR:$HOME/example/" >> $HOME/.bashrc;
source $HOME/.profile;

How can this be improved in terms of:

  • It does work, but are there cases where this will fail and how can I avoid them (i.e. are there any common processes or actions a ubuntu user will make etc. that will break my exported path)?
  • Are there any security concerns to this approach and how can those be fixed?
  • Are there any other collisions or negative consequences I have to pay attention to, in order to make this as universal as possible (within the ubuntu family)?
2
  • Couple of points you should address. 1) "Make the change accessible system-wide" . System wide changes for any user or just yourself ? From your example you are doing only for yourself. System-wide changes are done in /etc/bash.bashrc and /etc/profile. 2) " but are there cases where this will fail" . Which cases are we talking about ? Don't make people guess. Define your requirements clearly 3) " no logout required" part might not quite work for GUI, as gui sessions source ~/.profile on each login only once as far as i know, so they need to be restarted for change to take place Mar 17, 2016 at 21:55
  • Another thing. Here you are using FOOBAR as variable. So that's a path variable used by another ( external ) command ? because there exists $PATH variable , which is used by every shell and many programs Mar 17, 2016 at 21:57

1 Answer 1

3
  • it should be either ~/.profile or .bashrc. Not both since /.bashrc is loaded -from- ~/.profile.
  • A non-login shell will only load ~/.bashrc and not /etc/profile, ~/.bash_profile, ~/.bash_login or ~/.profile so if that is a concern ~/.profile is not going to be used.
  • a non-login with no shell will load the configuration set at $BASH_ENV. Have not found what is loads but ~/.bashrc should be one of them.

Regarding your 1st bullet point:

  • That is probably not a good idea. And probably not how it works. When you reload environment variables they will start from empty (assuming you meant it should not empty on reloading them ;-) ).

Regarding your 2nd bullet point:

  • if you want it for all users use /etc/profile or /etc/environment. /etc/environment is the correct place for system wide environment variables. And also: this rules out ~/.profile.

Regarding your 3rd bullet point:

/etc/environment does not require a reboot. It requires a re-login. And it should be done with a re-login; you can source it again but that only applies to your session and what is started after you sourced it. Not for already running processes.

Nevertheless

. /etc/environment

is a method to reload all variables. But again: this does not reset them for currently running processes. Only for the current shell and newly started processes.

Regarding 4th, 5th, 6th bullet point:

Only problem I can see is it could run wild. If you add it to ~/.bashrc and source that 10, 20 times your variable is going to get flooded with the same parts.

And no and no. No security risks.

18
  • Your link also mentions ~/.pam_environment as an user-based alternative to /etc/environment. If I create an assignment there (for example FOOBAR=/home/@{PAM_USER}/example/) it should be enough to satisfy my requirements. You mentioned that . /etc/environment can be used to reload paths for new processes (which is what I need). Is there an equivalent way of initializing the new values without a re-login for the PAM file?
    – user513183
    Mar 17, 2016 at 21:13
  • @mınxomaτ: Sounds like you should use ~/.profile after all, which you can source anytime using . ~/.profile. Apparently you don't need it to be accessible to other users. Mar 18, 2016 at 18:39
  • 1
    The advantage of /etc/environment method over others is with servers (and I consider it a better practice to use that over others just because you will do it correctly when using a server ;-) ). Use ~./profile if that fits your needs ;-) Or pam_environment. It is after all your system :D
    – Rinzwind
    Mar 18, 2016 at 18:52
  • 1
    But /etc/environment doesn't expand variables, and the syntax for variable expansion in ~/.pam_environment is not understood by e.g. bash. None of them is a true script file. Mar 18, 2016 at 18:54
  • 1
    @GunnarHjalmarsson if the objective is to affect programs or shell sessions, no matter how they are launched, then one is objectively better than the other for reasons given.
    – muru
    Mar 18, 2016 at 19:20

Your Answer

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