Setting environment variables

You use environment variables to set values that programs need. Specify the value of an environment variable by using the export command or the putenv() POSIX function. If you do not set an environment variable, either a default value is applied or the variable is not defined.

About this task

An environment variable defines some aspect of a user environment or a program environment that can vary. For example, you use the COBPATH environment variable to define the locations where the COBOL run time can find a program when another program dynamically calls it. Environment variables are used by both the compiler and runtime libraries.

When you installed IBM® COBOL for Linux® on x86, the installation process set environment variables to access the COBOL for Linux compiler and runtime libraries. To compile and run a simple COBOL program, the only environment variables that needs to be set is LANG, and it only needs to be set if you wish to use messages other than the default en_US messages.

You can change the value of an environment variable in either of two places by using the export command:

  • At the prompt in a command shell (for example, in an XTERM window). This environment variable definition applies to programs (processes or child processes) that you run from that shell or from any of its descendants (that is, any shells called directly or indirectly from that shell).
  • In the .profile file in your home directory. If you define environment variables in the .profile file, the values of these variables are defined automatically whenever you begin a Linux session, and the values apply to all shell processes.

You can also set environment variables from within a COBOL program by using the putenv() POSIX function, and access the environment variables by using the getenv() POSIX function.

Some environment variables (such as COBPATH and NLSPATH) define directories in which to search for files. If multiple directory paths are listed, they are delimited by colons. Paths that are defined by environment variables are evaluated in order, from the first path to the last in the export command. If multiple files that have the same name are defined in the paths of an environment variable, the first located copy of the file is used.

For example, the following export command sets the COBPATH environment variable (which defines the locations where the COBOL run time can find dynamically accessed programs) to include two directories, the first of which is searched first:


export COBPATH=/users/me/bin:/mytools/bin

Example: setting and accessing environment variables

Related tasks  
Tailoring your compilation