The concept of Environment Variables comes from the Unix world. They are used slightly in MS-DOS, but not at all to the same extent as UNIX. For the QDOS world, the file ENV_BIN provides a number of extensions that allow the use of Environment Variables.
Essentially an environment variable is a variable that can be “seen” by executable programs. In SuperBasic we can set up all kinds of variables, but if we execute a program from SuperBasic, these programs can not “see” these variables and get data from them.
The purpose of environment variables is to change the configuration of a program. They function like Config Blocks, but don’t require running a program to make the change.
Let’s take a quick look at how we can change the behavior of programs. There are five different ways of doing this:
- User Intervention. This is where the user uses a menu or answers queries from the program.
- Config Blocks. This feature is pretty unique to QDOS, but it allows the user to change default options without having to know how to edit a binary file.
- Configuration File. This is a separate file that the program reads to determine how to set defaults and run.
- Command line Arguments. Instead of the program querying the user for information, the user types it in when they execute the program.
- Environment Variables. The user sets a variable that is then read by the program and changes its default settings.
Each of the options have their own place and their own benefits and faults. Some are more permanent, like Config Blocks and Config files, while some are very short lived, like User Intervention and Command line Arguments. Enviroment Variables are in between as they can be set in a BOOT program, but they can be changed by just typing in a new command.
The ENV_BIN file comes with 4 extensions. They are:
SETENV - Defines an environment variable. ENV_LIST - Lists all defined environment variables. ENV_DEL - Deletes an environment variable. GETENV$ - Gets the value of an environment variable.
The two key commands are SETENV and GETENV$. SETENV is used like
this:
SETENV "VARIABLE=value"
SETENV takes a string argument of the type “XXXXX=YYYYY” where XXXXX and YYYYY are two strings separated by an equal sign. Any space before the equal sign is treated as a part of XXXXX and any space after the equal sign is treated as a part of YYYYY. The case of each string is important as “VARIABLE” is different from “variable”. By convention, upper case is used for variables.
The SETENV is done either in a BOOT or setup program or by the user. The command for an executable to get the contents of an environment variable is GETENV$. The command is used like this:
a$ = GETENV$("VARIABLE")
In this case a$ will be assigned the value of “value”. This comes from our previous SETENV command above. If the Environment Variable “VARIABLE” is not set (does not exist) then the GETENV$ would return a Null string ( “” ).
Now I did say that executables use GETENV$ and not SuperBasic programs. Since variables are already used in SuperBasic, we would not gain much in using environment variables. There the commands are use is in compiled SuperBasic programs, which are executables.
I see the purpose of using Environment Variables as adding to the flexibility of programs that use Config Blocks. Both Config Blocks and Environment Variables are really designed to change default program settings. User Intervention and Command Line Arguments are designed to tell the program what the data is. Using environment variables allows the user the ability of making a temporary change to the default options of a program, without having to go through the trouble of using “config”. Environment variables would be used to change a setting for a single session and that’s it.
Getting Config Blocks and Environment Variables to work together is not difficult. The program would first get it’s default settings from the Config Block. It would then check to see if there are any Environment Variables set. If there are, then the Environment Variable settings would override the Config Block settings.