BSD Newsletter.com
   Front | Info | Lists | Newsfeeds | Study Guide | What is BSD?
Advertisement: The OpenBSD PF Packet Filter Book: PF for NetBSD, FreeBSD, DragonFly and OpenBSD

BSD Links
·New Links
·Advocacy
·Drivers
·Events
·Flavours
·FAQs
·Guides
·Programming
·Security
·Software
·User Groups

This is the BSDA Study Guide Book written via a wiki collaboration. This is a work in progress. You may contribute to or discuss this specific page at http://bsdwiki.reedmedia.net/wiki/Recognize__44___view_and_modify_environmental_variables.html.

Recognize, view and modify environment variables

Concept

Be able to view and modify environment variables both temporarily and permanently for each of the default shells found on BSD systems.

Introduction

Environment variables are key-value pairs available to executing processes. By way of environment variables, users (and other processes) can pass data to new processes. Both keys and values can only be strings, and both are usually case sensitive. In shell scripts interpreted by /bin/sh (as well as many others), environment variable contents are referenced by ${KEY}.

Some environment variables need only to be set, without regards for their content (one example is the DEBUG variable), and there are usually command shortcuts for this operation.

Most shells have their own internal variables, which should not be confused with global environment variables as they are not passed to newly started processes.

Different shells have different commands for manipulating environment variables. Read more about their syntax in the appropriate manual pages.

sh, bash

Internal shell variables can be set simply by issuing a statement like "key=value", and inspected with the set command.. An internal variable can then be promoted to a global environment variable with the export command. Internal shell variables can be deleted with unset. If the internal variable was exported at the time, it will also be deleted from the environment.

Shell variables are only valid within a shell process instance (spawned subshells will not contain their parent's internal variables).

In order to just set an environment variable with empty content, use the form "export NAME" without defining the internal shell variable.

csh, tcsh

Internal shell variables can be set and inspected with the set command, and environment variables by the setenv command. Internal shell variables can be deleted with unset, and environment variables deleted with unsetenv command.

To set an environment variable to empty content, use setenv NAME.

Common environment variables

There are environment variables which have well defined meanings for a Unix process. Some of them are:

  • USER : Currently logged-in user (e.g. username)
  • HOME : Currently logged-in user's home directory (e.g. /home/ivoras)
  • TERM : Active terminal (console) type (e.g. xterm)
  • EDITOR : User's preferred text file editor (e.g. vi)
  • VISUAL : User's preferred visual file editor (e.g. emacs)
  • PAGER : User's preferred pager (e.g. /usr/bin/more)
  • PATH : User's search path for executables (e.g. /bin:/usr/bin:/usr/local/bin)

Examples

Automatically set and export an environment variable called "VEGETABLE" to "Carrot", in bash:

$ export VEGETABLE=Carrot

Create an environment variable called "VEHICLE" containing the string "Truck", in tcsh:

> setenv VEHICLE Truck

List environment variables, in tcsh:

> setenv

Note that (ba)sh uses "=" to set enviroment variables, and (t)csh doesn't.

Practice Exercises

  1. Investigate what does PWD environment variable do
  2. Experiment with setting the PAGER environment variable and the behavior of the manual page viewer (man)
  3. Investigate how does internal variable shlvl behave in (t)csh when spawning subshells
  4. Unset the PATH environment variable and see if you can start programs without specifying their full path

More information

env(1), sh(1), csh(1), tcsh(1), environ(7)



Front | Information | Lists | Newsfeeds