Please create an account to participate in the Slashdot moderation system

 



Forgot your password?
typodupeerror
×
Programming IT Technology

On the Use of Environment Variables? 20

ender-iii asks: "The company I work for is ready for a ground up revamp of our application. The old version relies heavily on environment variables (so much so, that if they aren't set, the app core dumps!). The old programmer wants to do the same thing and put all configuration in the environment. I am arguing for a config file. My question is this: why would anyone use environment variables for a standalone application?" I can think of lots of reasons where an environment variable for certain settings is better than having to edit a configuration file (user overrides of that setting, for example...and many time users aren't going to have write perms to config files!) however, they have the strongest value when used appropriately. If you are setting 10-20 variables in the environment, then you really should be thinking about a config file of some sort...not every admin/user will be happy cluttering their environment with unnecessary variables. What do you all consider appropriate use of ENV variables, and when is it acceptable to use it to store lots of config information?
This discussion has been archived. No new comments can be posted.

On the Use of Environment Variables?

Comments Filter:
  • by return 42 ( 459012 ) on Sunday July 01, 2001 @08:58AM (#116291)
    Why not let the user set one environment variable that points to a config file? Default value is the default file, but those who want to customize can copy the default file and point to their own. If you really want to avoid envs entirely, you could use a command-line option instead.
  • by winterstorm ( 13189 ) on Sunday July 01, 2001 @09:23AM (#116292)

    The utility of environment variables is indicated by the name. The environment that a program runs in should have influence on its behavhior. For instance the program should decide how many columns of text to display from a config file... what if the screen has 80 columns by the config file says 40? Environment variables should describe to a program what environment it is running in. Environment variables should answer quesitons like, "what is the name of the user running me?", "what is my current working directory?", "where should I find my config file?", "what kind of terminal am I running on? do I HAVE a terminal?".

    I think that there is nothing wrong with the same data being represented in a config file and the environment. In fact the best way is to have a global config file in /etc, a user specific config file (.somethingrc), and allow environment variables to override the config files. I disagree with the idea of replacing all your environment variables with a single once that just has the location of a config file. If your environment variables do indeed describe the working environment for the program then they should be in the environment.

    This isn't a slight or insult or sarcasm at all; you would probably find a book on the history of unix to be of benefit. You've obviously got the wisdom to ask, "why do we need these variables" when so many others just use them for EVERYTHING or use them for NOTHING. You'll get more insight from a book drawing your own conclusions than from us here.

    Oh, and if your program core dumps when a variable isn't set... that's just bad coding and you should be as suspicious as your are. :-)

  • by MadCow42 ( 243108 ) on Sunday July 01, 2001 @09:45AM (#116293) Homepage
    To me it sounds like your programmer doesn't want to have to parse a config file... not a hard thing to do though! I can't think of any other good reason why Environment Variables would be preferred...

    Now having said that, the question is whether the computer this runs on will be a "dedicated" workstation to THIS application... if so, you could get away with a ton of E.V.'s. If not, I'd hate to see the system cluttered up with unnecessary E.V.'s the rest of the time.

    On Windows, I prefer Registry Entries over config files, but that's a personal preference.

    MadCow.

  • ... based on over three years with the (commercial, non-open, non-free, cross platform) product I've been working on (and ported from NT to Solaris), and twenty years in the industry. The following advice applies to both Unix and other, less worthy, operating systems.

    Every configuration option must have a sane (and documented) default. Period.

    A configuration file has the following advantages:

    • If the user has a problem, he or she can send you the configuration file. This is easier than sending you an environment dump.
    • Users can more easily back up a configuraion file than all the combinations of things that set environment variables. (Eudora keeps everything it needs in one directory structure. Trivial to back up. Trivial to support multiple users on the same machine. Very nice.)
    • If your product can read the configuration file, perhaps your product can write the configuration file, too. Look at Sun's OpenLook mail tool, or at Z-Mail: both use (effectively) the .mailrc format, but provide (purely optional) GUI interfaces to set most or all of the options. Speaking as a one-time mailx guru, I found this much more convenient than surfing mailrc(1) options list (which I was, thank you very much, quite capable of doing).

    Environment variables have the advantage of really simple ad hoc experimentation:

    $ MAGICFOO=bar yap # or
    % env MAGICFOO=bar yap


    We use one environment variable, which points to the directory where our configuration file lives.

    Don't talk to me about "the registry". Our product doesn't use the registry. Eudora doesn't use the registry. Both greatly benefit as a result of this non-use. The registry is a black hole: everything falls into it, because it really sucks.
  • by ryants ( 310088 ) on Sunday July 01, 2001 @11:10AM (#116295)
    The old version relies heavily on environment variables (so much so, that if they aren't set, the app core dumps!)

    Uh... forgive me for sounding crass, but if that's the case, you probably have bigger battles to fight than env. variables vs. config files.

    Ryan T. Sammartino

  • I hate environment variables, because they're just not as convenient as a config file.

    1) A config file can be edited with vi. Environment variables are usually tinkered with at the command line. vi is a better place to do a lot of typing, especially if you're using the Bourne shell.

    2)Environment variables are listed with the set command, which dumps them in a particular order, namely alphabetical. Now, lots of config vars are heiarchal in nature, with things like the global dir set first, then some subdirs depending on the global dir variable, and then maybe specific program configs at the lowest level, depending on directory. A config file lets you organize this in a better way, and use format to give the user some clues.

    3) All my bad memories of trying to install some massive 5 gigabyte program across several servers with hundreds of files and directories come down to one thing: I had to check every single environment variable by hand and make sure that it was correct. Who feels like finding the mistake in something like PROGRAMDIR=/opts/one/two/three/four/five/zix/seven /eight/nine/ten/oracle/needs/this/special/path/set /right/here/no/maybe/it/is/right/here/yes/i/think/ it/is/definitely/foo

    4) lastly, config files can have COMMENTS in them. In fact, you can build your configuration file so that no external documentation is required. Look at the file, read the comments, and make the program work.

  • Having worked on many platforms, I can honestly say that if your program works under dos and is meant to be used by stupid people then use environment variables. Say you manage 20 stores, and each store uses about 5 programs. Every time you set a store up you edit 2-3 variables in one autoexec (or other batch file) and you have configured the entire store, instead of messing with 5 configuration files. Not only that but your other batch files can branch according to the store config which is set in the environment. Of course, if your market is hard core programmers and sys admins, then it would probably be best to use a config file (eh, in my day we didn't have config files, you started the program, found it in memory, and changed the values on the fly!), on the other end of the spectrum you'll have people (on all platforms) who are best served by a simpler environment variable.

    This is far easier than having to write a program which parses someone else's config file, returning an error code, and then branching the batch based on the error code returned, or, even worse, storing the required info in both a variable and config file.

    Ideally you'll give the user a choice, as suggested above, by allowing usage of environment variables, config file, command line, default config file, and program defaults.

    But it sounds to me as though 1) you haven't asked the guy WHY he wants to stick with environment variables or 2) he doesn't have a good answer. Backwards compatability is a good answer, though, and it's not to hard to maintain backwards compatability and put a config file and command line options in there as well.

    -Adam
    This sig 80% recycled bits, 20% post user.
  • Oh, and another thing.

    DON'T make an environment variable that contains the path to the configuration file.

    Put the damn config file into /usr/local/etc where it belongs, and have your program look there and only there.

    Thank you very much for the opportunity to rant.

  • by dutky ( 20510 ) on Monday July 02, 2001 @06:02AM (#116299) Homepage Journal

    Here is the policy that I try to follow in my programs:

    1. Default Values: Provide reasonable default values, hard coded into the program, in case the program is run without any configuration.
    2. Config Files: Search for a configuration file in a number of reasonable locations and override any default values with values from the configuration file.
    3. Envronment Variables: Override default values or values from the config file with anything found in the environment.
    4. Command Line Parameters: Override everything else with anything specified explicitly on the command line.
    As an added bonus, I try to provide a command line flag that will cause the program to dump the current configuration to a file, along with as much documentation (your configuration file format should allow for comments so it can be documented in situ) as is needed to understand and edit the file.

    Only in extreme circumstances should the program come to a point where it cannot proceed because of missing configuration information. If it does come to such a point, it certainly shouldn't core dump. Rather, it should display an informative error message, indicating which configuration parameters are missing and giving some advice on how to either obtain a basic configuration file or set the required paramter.

  • Just a case of a lazy programmer not checking the results of getenv(). That shouldn't be too hard to fix.

  • Another major disadvantage of environmental variables would be that they go AWOL when one logs out or the machine is rebooted. If there are a lot of these, I'd bet they're being reinitialized in the .profile on login, right? So what do you have there?

    A (badly-done) config file. ;)

  • Environment variables are nice because they give you an easy way to pass different configuration to multiple instances of the program. (Argument vectors can do the same thing, but are not always as convenient: for example, what if you have a whole suite of related programs that share configuration information?) On the other hand, you probably don't want to pass a large number of settings via environment variables.

    One popular combination is to use an environment variable to point the program suite to a configuration file or directory of configuration files.
  • by tzanger ( 1575 ) on Sunday July 01, 2001 @03:26PM (#116303) Homepage

    DON'T make an environment variable that contains the path to the configuration file.
    Put the damn config file into /usr/local/etc where it belongs, and have your program look there and only there.

    Bah. I put most of my personal installs into /opt/ so /usr/local/etc does nothing for me. Yeah I could symlink but that's a pain; why not allow $CONFIG_PATH and default to /usr/local/etc?

    Your post comes along as so much old-timerism that I can smell the Ben Gay from here.

  • Environment varioables are also edited with VI, in your .profile, .login, or .cshrc file. Unless you're testing, you never log in and have to type things like "export HOME=/home/tbone".

    Environment variable are dumped in whatever order they were pushed into the environment stack. Unless the shell reorders your environment for you, most shells do not alphabetize your vars. Besides, if you're going to use the environment to store variables in, you should be using a syntax like PREFIX_VARIABLE=value. Then you can 'set | grep PREFIX' | mail -s "Environment problems" suport@company.com" and get an answer back.

    You mean you had to edit your PATH variables? God forbid you actually do sysadmin work and set up your paths for your software....

    Comments are for weenies :)

    In all seriousness, Env variables should be part of a larger scope of things. Parsing configurables should work like this:
    • Set up the defaults for your configurables.
    • Read the global config file, reset anything that's changed
    • Read the user config file, reset anything that's changed
    • Opent he environment, read in any variables that are set there, and reset andthing that's changed

    It's not exactly efficient (4 config passes), but it's not like you're reading the Windows Registry 4 times or anything.


    This space for rent. Call 1-800-STEAK4U

  • This is technically off topic to the exact question at hand, but may be relevant to some readers. One successful system I implemented for work had both; because it was itself a shell script, all its variables were environment variables. This led to some unforseen advantages.

    This was a utility that I chose to implement as a couple of bash scripts. It stored and retrived the state of the system in a config file consisting of environment variable assignments, which it could in turn source back in. The big unforseen benefit was that the system could change its state by making edits to the file, then sourcing it in again, and it would automagically inherit the self-consistent edited new state. (So long as the edits were logically correct, at any rate; but I didn't have to make parallel edits to the config and to the runtime state.)

    I admit again, this doesn't map strongly to the exact literal question at hand. The parts of this design that did are:

    • the system was installed by modification to, at most, $PATH, in the users' environments; and
    • the script itself contained a hardcoded path to its metaconfig file in a standard location.

    Hope someone finds this useful.

    --

  • by coyote-san ( 38515 ) on Sunday July 01, 2001 @03:26PM (#116306)
    My rule of thumb on this is simple - if it only affects one program (or a related suite of programs), it should go into a config file.

    Environment variables should only be used for things that are interesting to multiple, unrelated applications. Things like PAGER, EDITOR or VISUAL.

    If you want to have the ability to override the values compiled into the executable or in the config file, use "-Dname=value" and "-Uname" on the command line. Only check the environment variables for this if there's a compelling reason to do so. (E.g., "CFLAGS" has been supported for so long that no C compiler or preprocessor can reasonably ignore it.)

    Config files aren't quite as convenient as environment variables for some types of coding, but the flip side is that you don't have to worry about name collisions in the environment variable namespace, platforms that impose limits on the number of environment variables, etc.
  • Put the damn config file into /usr/local/etc where it belongs, and have your program look there and only there.

    Don't you mean "put the system default config file in /etc/<pgmname>/default.conf and the user-definable overrides in $HOME/.<pgmname>.conf?

  • Your post comes along as so much old-timerism that I can smell the Ben Gay from here.

    Damn. I'm 32 years old, but thank you for the compliment.

    "And we LIKED it too!"
  • by cr0sh ( 43134 ) on Sunday July 01, 2001 @09:46PM (#116309) Homepage
    And one I might be modded down for, if only for being "buzzword compliant":

    Use a config file, of course - but use XML as the format for that config file.

    The main reason is for human readability, as long as you make your node names, etc - sufficiently readable. Furthermore, you could include nodes that would allow for user defined settings (if that is the reason that environment variables were being used) - these nodes could specify where to look for the user's XML config file (say, in $HOME/appname/userconfig.xml) - which the user could edit easily - if the config file is absent (or the path doesn't exist, etc) - the parser could supply default values (perhaps picked out from the main config file - which the user couldn't edit).

    Using XML might make things more difficult (especially if you have to write your own parser) - but if you are writing from the ground up anyhow, you might as well set up to use XML, because your app will probably need it (intercommunication between different app sections - especially if you are black boxing everything to make parts very reusable. Also if you are using an XML format that is well defined, you can easily create back ends for any data sharing for third party apps that might be needed).

    Worldcom [worldcom.com] - Generation Duh!
  • Actually, the best of both worlds would be to have the ability to set things via command line OR environment variables OR by a settings file; in the cases where this is already done on unix, it's usually the case that variables are munged in in the following order of importance: command-line parameters > environment variables > config files > default values. Yes, this means that you have to parse 3 different sets of instructions, but using a structure like a hash to store these settings will make it easy to find which is the value the user wants.

He has not acquired a fortune; the fortune has acquired him. -- Bion

Working...