LINUX TIPS AND TRICKS --- May 12, 2000

Published by ITworld.com, the IT problem-solving network
http://www.itworld.com/newsletters

*********************************************************************

Creating And Accessing Environment Variables
By Danny Kalev

An environment variable consists of a pair of strings: the variable 
itself followed by = and its associated value. For example:

  HOME=/home/susan

To list all existing environment variables, type the following command:

  env

Alternatively, use the "echo" command to display the value of a single 
environment variable:

  echo $MY_IP

Don't forget the preceding $ sign before the variable's name.

DEFINING ENVIRONMENT VARIABLES
You can define an environment variable from the command line using an 
assignment expression. The following example creates an environment 
variable called SERVER_VER whose value is Apache1.1.3:

  SERVER_VER=Apache1.1.3

By default, the scope of an environment variable defined in this way is 
limited to the current shell. If you want to make an environment 
variable globally visible, you have to export it like this:
  
  export SERVER_VER=Apache1.1.3

An environment variable definition can be recursive -- you can use an 
existing variable to define a new one:
  
  MY_DIR=$HOME/projects/src 

ACCESSING ENVIRONMENT VARIABLES IN A PROGRAM
You can also access environment variables from a program using system 
calls. A program's environment is made available to it at startup 
through the global pointer environ (defined in <unistd.h>), which 
points to an array of C-strings in the form "VAR=value". The ANSI C 
function getenv() provides an easy and portable way to access an 
environment variable. This function takes an environment variable's name 
and returns a pointer to its value. If the variable is not defined, it 
returns NULL:

#include <stdlib.h>
#include <stdio.h>
int main()
{
  char * descr = getenv("PATH");
  if (descr)
    printf("value of PATH is: %s", descr);
  else 
    printf("variable not defined");
}

To create an environment variable, use the putenv() function. This 
function takes a string in the form "VAR=VAL". putenv() adds the 
variable VAR to the current environment and assigns the value VAL to it. 
If the variable already exists, putenv() will assign the new value to 
it. If you prefer not to overwrite an existing variable's value, use the 
setenv() function instead. setenv() takes three parameters: a variable, 
a value, and a flag indicating whether the function should overwrite an 
existing value or not:

  setenv("MY_IP", "213.123.67.99", 0);

The third argument, 0, indicates that sentenv() shouldn't modify the 
value of MY_IP if it already exists. To force overwriting, use 1 instead 
of 0.

************************************************************************

COMMUNITY DISCUSSION

Talk about Linux

If this column has raised questions or comments, let's hear them. 
Join author Danny Kalev's Linux Software Development discussion on 
ITworld.com Forums. This week: MS isn't the only monopoly. Oracle and 
AOL are closing in fast. 
http://forums.itworld.com/webx?14@@.ee6b652/30!skip= 

About the author
----------------
Danny Kalev is a system analyst and software engineer with more
than 10 years of experience, specializing in C++ and
object-oriented analysis and design on various platforms including
VMS, DOS, Windows, Unix, and Linux. His technical interests involve
code optimization, networking, and distributed computing. He is
also a member of the ANSI C++ standardization committee and the
author of ANSI/ISO C++ Professional Programmer's Handbook (Que,
1999). Contact him at linuxnl@excite.com.
 
************************************************************************
