process environment

From NetBSD Wiki

Jump to: navigation, search

The process environment is simply a list of name/value pairings that can be used for a rudimentary form of communication between processes.

The environment is strictly a one-way communication, and it can only be used to pass information to a process at startup. A process can send environment information to its children, but not the other way around.

What happens is that a process first modifies its own environment with setenv and then calls one of the exec functions to start a new process from an executable. This process will inherit the environment as it was in the parent (it is simply passed on the stack just before the arguments to the new command).

The most common use of the environment is to pass preferences from the shell to processes started in interactive mode. For example, the $PAGER environment variable tells several programs which pager you prefer, so they can start that instead of some arbitrarily hardwired default. In bourne-like shells you can set the environment variable like so:

$ export EDITOR=/bin/vi

and you can get at its value by just referencing it:

$ echo $EDITOR
/bin/vi

You can view the environment as seen by a newly run process by invoking env. You can also use this command to start a process with an environment variable set to a different value without affecting the calling shell.

Common environment variables

  • EDITOR and VISUAL determine which editor to use. The typical algorithm first checks VISUAL, and only if that is not set, it checks EDITOR. If neither is set, vi is usually the default.
  • PAGER selects which pager should be used by programs that produce lots of pages of output (like man).
  • PATH is looked at by the shell and certain other programs which need to start applications. If you type in the name of an executable program without an absolute path in front of it, it will look in the directories specified in PATH (separated by commas)
  • MAIL is the name of the file where your user's mail is delivered on the system. Programs like mail look at this value.
  • SHELL is set by most shells. It contains the name of the currently invoked shell, which is useful in shellscripts that need to do shell-specific tricks.
  • TERM has the name of the terminal, which is used in termcap/terminfo lookups to determine what the display can do (color/no color, full-screen refreshes, backspaces etc). This is set by getty or your terminal emulator (like xterm, but also GNU screen).
  • HOME is your user's home directory.
  • USER contains the username of the currently logged-in user. note: this is purely informational, it is NOT the absolute truth. Use whoami or something like that if you need to know for sure.

See also

Personal tools