As a system administrator with root access on a unix machine, is it possible to provide default shell variables to all users of a system?

That is, as an individual unix user, I can define environmental variables in my bash shell by editing my .bash_profile

export FOO="BAR"

Is there a way to provide all users with a default set of environmental variables without editing each user's individual shell variables? If there a way to provide specific users with default variables?

Long time *nix user here, but I've never done much on the administering a unix machine side, so my apologies if this is a dump/naive question.


If am understanding your question correctly, you need system wide environmental variables that can be accessed by all users in the system. If so, you could set it from /etc/profile. From here,

The /etc/profile file is not very different however it is used to set system wide environmental variables on users shells. The variables are sometimes the same ones that are in the .bash_profile, however this file is used to set an initial PATH or PS1 for all shell users of the system.

  • Yes, you understood my very basic question correctly, thank you.
    – Alan Storm
    Oct 31 '14 at 16:35

For setting global variables, there may a number of approaches, depending on your users' shells, and whether or not your system supports a login.conf capability database or similar, which should apply, no matter what shell a user has.

The simplest approach is to add the definitions you want to the relevant system files under /etc. Depending on the shells used on the system, you may find yourself setting up and managing multiple files. For example, sh and bash probably expect to find /etc/profile, zsh looks for /etc/zprofile, whereas csh or tcsh will look for something different.

If your system supports the capability database, you only need to modify one file, and all settings should be applied correctly. Under FreeBSD, for example, you'd add your envar definition to the setenv capability for any class of user that needs it:


Rebuild the database, and at next login $FOO should be set.

If you don't have login.conf, there may be a different method of globally configuring users' environments on your system.

To set variables for a particular user, I think you'd need to use the first approach, where you'd test the value of the $USER variable in a case statement or similar construct supported by the particular shell. For example, this should work in sh and compatible:

case $USER in
    root) FOO=ROOTBAR;;
    *) FOO=USERBAR;;
export FOO

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.