Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

This question already has an answer here:

I want to know how the standard environment variables given by the linux OS like PATH, HOME are set automatically. In which file are these read from. There should be some file from which these variables are set when a particular user login.

share|improve this question

migrated from Sep 22 '11 at 11:41

This question came from our site for system and network administrators.

marked as duplicate by Gilles linux Sep 8 at 21:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

As additional lecture you could read From Power Up To Bash Prompt. Old, but gives a good introspection. –  manatwork Sep 22 '11 at 12:19

5 Answers 5

up vote 9 down vote accepted

The answer to your question can be found in INVOCATION section of man bash. Here's relevant excerpt:

   When  bash is invoked as an interactive login shell, or as a non-inter-
   active shell with the --login option, it first reads and executes  com-
   mands  from  the file /etc/profile, if that file exists.  After reading
   that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
   in  that order, and reads and executes commands from the first one that
   exists and is readable.  The --noprofile option may be  used  when  the
   shell is started to inhibit this behavior.

   When  a  login  shell  exits, bash reads and executes commands from the
   file ~/.bash_logout, if it exists.

   When an interactive shell that is not a login shell  is  started,  bash
   reads  and executes commands from ~/.bashrc, if that file exists.  This
   may be inhibited by using the --norc option.  The --rcfile file  option
   will  force  bash  to  read  and  execute commands from file instead of

   When bash is started non-interactively, to  run  a  shell  script,  for
   example, it looks for the variable BASH_ENV in the environment, expands
   its value if it appears there, and uses the expanded value as the  name
   of  a  file to read and execute.  Bash behaves as if the following com-
   mand were executed:
          if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
   but the value of the PATH variable is not used to search for  the  file

There's even more in the man page, I recommend you read it.

share|improve this answer
what if he's not using bash? –  frogstarr78 Sep 29 '11 at 1:48
man page for ksh also has Invocation section. csh has Startup and shutdown, which describes files read by starting shell. I expect other shells also have relevant sections in their manuals. –  Paweł Brodacki Sep 29 '11 at 6:21

Things are a little more complex than just what your shell provides.

There are three main ways to login:

  • login from a true terminal (nowadays, mainly the console)
  • login from a pseudo-terminal (mainly network connections)
  • login from a graphic environment

They all are able to set up the environment before executing your shell, and they all do (HOME, LOGNAME, TERM are probably not set by your shell, and even if you have no start up files, your shell get probably also a PATH).

When login from a true terminal, the process handling the connection will probably set up the TERM environment variable and delegate the rest of the work to the login program. That program does some verifications (such as preventing login for root on unsecure terminals), ensures that the whole environment excepted TERM is clean, initializes HOME, PATH, SHELL, TERM, MAIL, and LOGNAME and then launch you shell as a login shell. Your shell will then do its own initialization.

When login from a pseudo-terminal, the same things happen with a little twist. Often the environment get more initialization form the process handling the connection that just the TERM environment variable (network protocols often have a way to transfer the environment from the other side) and thus login is used in a mode in which it doesn't clean the environment, the clean up is done by the program handling the connection.

Graphic environments usually don't delegate to login but behaves mostly the same. After verifying your credentials, they create a clean environment (with usual environment variable and at least DISPLAY set correctly, they often allow a sysadmin provided script to add things) then launch the start up script for your desktop environment; those may try to get the environment from your login shell and also often provide the possibility to provide a script to complete the set up. So when you launch any program from your desktop, it get an environment which is the combination of what the graphical login program, your desktop environment and your login scripts set.

A last thing, when you launch a terminal emulator, you may get either a login shell (in which case the login script from the shell is executed and thus you get to see the effect of your latest changes in it, but won't see the same environment as the other programs) or not (in which case the login script from the shell isn't executed, you don't see your latest changes, but you get the same environment as the other programs — modified by the interactive initialization script of your shell).

share|improve this answer

check /etc/bash* /etc/profile* files.

share|improve this answer
+1 for a solution that would work for other shells other than bash ONLY –  frogstarr78 Sep 29 '11 at 1:48

You should take a look at the .profile file in the root of user directories - variables like those can be set (or overridden) there.

However, if you mean where do all environmental variables get set - I'd assume that they get inherited from some parent process. Perhaps a look at something like this might help.

share|improve this answer

Check the man page for bash as it should give you all the files that are checked (and in which order) depending on your distribution. The env command will tell you what is set -- but not where from. However, a quick grep will tell you where those are set.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.