How can I retrieve environment variables in COBOL?

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Even though COBOL is an old language, it is still used in many critical systems. But how do you adapt its behavior depending on the environment, for example, switching from development mode to production mode? The answer lies in environment variables.

Article published on 02/03/2026, last updated on 10/08/2026

What are environment variables?

An environment variable is a value stored by the operating system and accessible by programs.

They are used to define an application's execution context:

  • server name
  • path to a database
  • operating mode (DEV / PROD)
  • etc...

In COBOL, you can access them using the ACCEPT instruction.

Reading an environment variable in COBOL

Here's a simple example to retrieve the environment variable APP_MODE:

       IDENTIFICATION DIVISION.
       PROGRAM-ID. EnvExample.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  APP-MODE       PIC X(10).

       PROCEDURE DIVISION.
           ACCEPT APP-MODE FROM ENVIRONMENT "APP_MODE"
           DISPLAY "Mode d'exécution : " APP-MODE
           STOP RUN.

  • ACCEPT allows you to read an external input.
  • By specifying FROM ENVIRONMENT "APP_MODE", we ask COBOL to read the corresponding environment variable.
  • The value is stored in the APP-MODE variable.

Practical example

Let's imagine you want to execute certain instructions depending on the environment:

       IF APP-MODE = "DEV"
           DISPLAY "Connexion à la base de test..."
       ELSE
           DISPLAY "Connexion à la base de production..."
       END-IF.

But before running your program, you can set the variable according to the context:

Mode d'exécution : DEV
Connexion à la base de test...

Handling special cases

Numeric values

One thing to remember is that an environment variable is always passed to a program as a string. It's possible to convert it automatically in COBOL, by declaring it as a numeric value, like this with port which would equal 3000:

01 PORT PIC 9(4).
ACCEPT PORT FROM ENVIRONMENT "PORT"

But this isn't recommended.

If your variable mistakenly contains a value that can't be parsed, then there's a good chance your program will crash (depending on your environment).

It's better to read an alphanumeric value, then check and convert it, like this:

01 PORT-STR PIC X(10).
01 PORT     PIC 9(4).

ACCEPT PORT-STR FROM ENVIRONMENT "PORT"

IF PORT-STR IS NUMERIC
    MOVE PORT-STR TO PORT
ELSE
    DISPLAY "PORT invalide"
END-IF

Empty values

If an environment variable is read, but it's empty (or simply not defined), then your program will only receive a set of spaces equivalent to the length of your variable.

To check whether your variable is defined or not, and assign it a default value, all you need to do is this:

ACCEPT APP-MODE FROM ENVIRONMENT "APP_MODE"
IF APP-MODE = SPACES
    MOVE "DEV" TO APP-MODE
DISPLAY "Mode d'exécution : " APP-MODE
STOP RUN.

The SPACES constant is a "figurative" constant, which adapts to the context, and corresponds to the number of spaces needed to fill your variable.


Finished reading this article?
Our complete courses
Take it to the next level with our courses!

Complete courses, exercises and certificates to really learn programming!

4.8 average rating

Comments (0)

to leave a comment

No comments yet