Windows environment variables in detail

An environment variable must start with a variable

Since is a variable, a change in its value can change the results of a program, and the value of a variable may be changed during execution. Variables can be set, called, and cleared using different syntax. On a computer, it is a behavior that affects the process it runs. Each application references environment variables, such as setting the home directory to an environment variable when the computer logs on.

Windows Common Permission When you open CMD or PowerSehll programs, such a directory is opened by default. %USERPROFILE%

Windows Administrator permission When you open CMD or PowerSehll, the default directory %SystemRoot%\system32 will be opened

Because the computer is looking for the directory structure owned by the user running the process and displaying it. CMD or Powershell processes opened in different directories must have different directory structures for different users with different permissions, so the directories displayed are different. Opening a terminal on Linux or Mac will display the HOME variable.

True environment variable

A process is an instance of a computer program executed by one or more threads. It contains program code and its activities. Depending on the operating system, a thread can consist of multiple threads of execution of concurrent instructions. Multiple processes may be associated with the same program. For example, opening multiple instances of the same program often results in multiple processes being executed. A Chrome application that opens multiple sites (instances) with tabs or new Windows increases the number of processes in the task manager.

Each process has an environment block that contains the names of a set of environment variables and their values. Environment variable has two types: the user environment variable (for a user in the system Settings, other users can’t use) and the system environment variables (Settings for all users, all users share) when the application during initialization, access to a process, and through the environment variable name it is possible to load and access them. By default, a child inherits its parent’s environment variables. For example, a program started by a command processor inherits the command processor environment.

For example, run the following command in Powershell

CD C:\Users // Environment variable =C: that is, the current drive, set it to C:\Users // Now win32 directory set to C:\Users D: CD D:\Program Files // Environment variable =D: // Now the win32 directory is set to D:\Program Files A: // The current win32 directory is set to C:/Users DIR // show the file directory structure under C:/UsersCopy the code

When we switch back to drive C, we ask the command processor, what directory did we keep when we last accessed drive C? It looks at its environment and finds the =A: variable, and gets the current directory of the current drive, which is to set the Win32 directory to the current directory. The values of these variables are exported to the environment because we want the child process to inherit “the current directory of each forged drive”

An environment variable, such as Path, is equivalent to a specified variable that is defined in the context in which it is called. If defined, the value of the variable is displayed. Path looks for the command you typed and the program corresponding to the command. If you want to keep some applications in the bin folder in their home directory, you must add that directory to your Path so that the operating system can look for applications to run in it when it issues commands.

In PowerShell, each environment variable is represented by an object. The name of an environment variable is a dictionary key, and the value of an environment variable is a dictionary value. The following example changes and displays Path for PowerShell

Get-Item Env:Path
$Env:Path
$Env:Path+ ="; C:\test"
Set-Item -Path Env:path -Value ($Env:Path + "; C:\test")
Copy the code

The change here is only valid in the current session, that is, the temporary environment variable (not valid in a new CMD or Powershell) is similar to the Set command in CMD. Permanent changes require that changes be stored in a registry, and environment variables can be changed and applied in advanced system Settings.

Pseudo-environment variable

Dynamic environment variables are pseudo-environment variables supported by CMD when command line extension is enabled. Their values can be changed but are not stored in the environment.

echo %DATE%
echo %TIME%
Copy the code

Afterword.

The reason you need to manually set the environment variable Path every time you install some programming environment is because you need to run a command or program in a command interpreter such as CMD or Powershell that doesn’t exist in the current environment. Therefore, the following error is reported.

"XXX"Not internal or external commands, not runnable programs or batch files. XXX: The "XXX" item cannot be recognized as the name of a CMdlet, function, script file, or runnable program. Check the spelling of the name, and if the path is included, make sure the path is correctCopy the code

If I had thought about why NPM sets the package installation path once, all programs or commands in its folder can be used all the time, and some NPM packages can still use the same commands even if they are upgraded, I might have figured it out sooner.

So, most of the time we focus on what environment variables do on Windows, mainly because you can’t be bothered to set Path, and the command interpreter just goes blind with black question marks. “You say of this thing where? Not here? This knot in one’s heart I all found over, you change a directory, or be you didn’t install this software, anyway this pot I don’t carry on the back”

This article was automatically published by ArtiPub