ArticleslgStudy

science

Environment variable

Environment variable is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Environment variable rather than just read about it. In short: An environment variable is a user-definable value that can affect the way running processes will behave on a computer. Environment variables are part of the environment in which a process runs.

Key takeaways

  • Environment variable belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Environment variable to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Environment variable from memory before moving on to harder problems.

Reference excerpt

An environment variable is a user-definable value that can affect the way running processes will behave on a computer. Environment variables are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process. They were introduced in their modern form in 1979 with Version 7 Unix, so are included in all Unix operating system flavors and variants from that point onward including Linux and macOS. From PC DOS 2.0 in 1982, all succeeding Microsoft operating systems, including Microsoft Windows, and OS/2 also have included them as a feature, although with somewhat different syntax, usage and standard variable names.

Design In all Unix and Unix-like systems, as well as on Windows, each process has its own separate set of environment variables. By default, when a process is created, it inherits a duplicate run-time environment of its parent process, except for explicit changes made by the parent when it creates the child. At the API level, these changes must be done between running fork and exec. Alternatively, from command shells such as bash, a user can change environment variables for a particular command invocation by indirectly invoking it via env or using the ENVIRONMENT_VARIABLE=VALUE <command> notation. A running program can access the values of environment variables for configuration purposes. Shell scripts and batch files use environment variables to communicate data and preferences to child processes. They can also be used to store temporary values for reference later in a shell script. However, in Unix, non-exported variables are preferred for this as they do not leak outside the process. In Unix, an environment variable that is changed in a script or compiled program will only affect that process and possibly child processes. The parent process and any unrelated processes will not be affected. Similarly, changing or removing a variable's value inside a DOS or Windows batch file will change the variable for the duration of COMMAND.COMor CMD.EXE's existence, respectively. In Unix, the environment variables are normally initialized during system startup by the system init startup scripts, and hence inherited by all other processes in the system. Users can, and often do, augment them in the profile script for the command shell they are using. In Microsoft Windows, each environment variable's default value is stored in the Windows Registry or set in the AUTOEXEC.BAT file. On Unix, a setuid program is given an environment chosen by its caller, but it runs with different authority from its caller. The dynamic linker will usually load code from locations specified by the environment variables $LD_LIBRARY_PATH and $LD_PRELOAD and run it with the process's authority. If a setuid program did this, it would be insecure, because its caller could get it to run arbitrary code and hence misuse its authority. For this reason, libc unsets these environment variables at startup in a setuid process. setuid programs usually unset unknown environment variables and check others or set them to reasonable values. In general, the collection of environment variables function as an associative array where both the keys and values are strings. The interpretation of characters in either string differs among systems. When data structures such as lists need to be represented, it is common to use a colon (common on Unix and Unix-like) or semicolon-delineated (common on Windows and DOS) list.

Syntax The variables can be used both in scripts and on the command line. They are usually referenced by putting special symbols in front of or around the variable name. By convention, names of environment variables are normally expressed in all capital letters. This helps keep environment variables distinctly different from other variables and identifiers used in programming codes. Nevertheless, note that case sensitivity in environment variable names differs between operating systems. That is, Unix-like operating systems are case-sensitive with respect to environment variable names, while DOS, OS/2, and Windows are not case-sensitive.

Unix In most Unix and Unix-like command-line shells, an environment variable's value is retrieved by placing a $ sign before the variable's name. If necessary, the name can also be surrounded by braces. To display the user home directory, the user may type:

In Unix and Unix-like systems, the names of environment variables are case-sensitive. The command env displays all environment variables and their values. The command printenv can also be used to print a single variable by giving that variable name as the sole argument to the command.

DOS, OS/2 and Windows In DOS, OS/2 and Windows command-line interpreters such as COMMAND.COM and CMD.EXE, an environment variable is retrieved by placing a % sign before and after it. In DOS, OS/2 and Windows command-line interpreters as well as their API, upper or lower case is not distinguished for environment variable names. The environment variable named HOMEDRIVE contains the drive letter (plus its trailing : colon) of the user's home directory, whilst HOMEPATH contains the full path of the user's home directory within that drive. So to see the home drive and path, the user may type this:

The command SET (with no arguments) displays all environment variables and their values. In Windows NT and later set can also be used to print all variables whose name begins with a given prefix by giving the prefix as the sole argument to the command. In Windows PowerShell, the user may type the following:

or one of the following redundant equivalents:

In PowerShell, upper or lower case is not distinguished for environment variable names. The following command displays all environment variables and their values:

Assignment: Unix The commands env and set can be used to set environment variables and are often incorporated directly into the shell. The following commands can also be used, but are often dependent on a certain shell.

VARIABLE=value # (there must be no spaces around the equals sign) export VARIABLE # for Bourne and related shells

export VARIABLE=value # for ksh, bash, and related shells

setenv VARIABLE value # for csh and related shells

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Environment variable

Start with the simplest possible case. Write down what Environment variable claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Environment variable before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Environment variable ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Environment variable

In research
Environment variable appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Environment variable in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Environment variable is common in secondary-school and first-year university syllabi. It links to neighbouring topics Environment variables, Operating system technology, Windows commands, so understanding it makes those chapters shorter.
In everyday life
Look for Environment variable outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Environment variable” →

How to study Environment variable in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Environment variable means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Environment variable out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Environment variable in simple terms?

An environment variable is a user-definable value that can affect the way running processes will behave on a computer. Environment variables are part of the environment in which a process runs.

Why does Environment variable matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Environment variable?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Environment variable.

Tags

  • Environment variables
  • Operating system technology
  • Windows commands

Keep exploring