Tuesday, March 15, 2011

When to use environment variable or command line parameter?

When to use environment variable or command line parameter?

I think there are two ways to pass parameters to another process in a script ,environment variable and command line parameters.

Thus,in which condition we choose one instead of another?

From stackoverflow
  • Hi ddbb,

    I suggest you prefer command line parameters over environment vars unless

    • you need values that persist across commands.
    • you need system wide global values

    Foredecker

  • Always prefer parameters when you can.

    But say you have Script1 that calls Script2. Both scripts are distributed to many users. Script2 may need different parameters depending on the user, and the users don't want or are unable to alter Script1 for some reason. This is a case where environment variables might be useful.

  • In most of the scripts I write, I allow both with the command line parameters taking preference.

    This is to allow 'lazy' users who want to set'n'forget the parameters to do so.

    It also allows over-riding of those parameters in special cases by the command line.

    For those that don't want to take the chance that their parameters might be set up incorrectly, they can just use parameters.

    Sometimes I'll even have more levels in the hierarchy, in order of precedence:

    • Value set while program running.
    • Command-line parameter.
    • Environment variable.
    • Local config file.
    • Global config file.
    • Default.

    That way, for each variable, you just work your way up that list, setting it to the relevant value, if it's there.

0 comments:

Post a Comment