野声

Hey, 野声!

谁有天大力气可以拎着自己飞呀
twitter
github

Why do we need to configure environment variables?

When I was learning Java before, I felt that the most difficult thing to do was to configure the environment for JDK. It was really difficult, and I had a deep impression of Path, JAVA_HOME, and CLASSPATH... (But now with JDK11, there is no need to configure classpath anymore, jre and jdk have been merged).
Last summer vacation, when I needed to set up the environment for OpenCV, there were still many things to configure, which deepened my understanding of environment configuration.

Now I understand more, and I have configured many environments on different platforms. Now I want to share my feelings about environment configuration.

Let's talk about the problem of environment configuration on Windows, which is similar to Linux.

Why do we need environment variables#

I remember that our computer networking teacher mentioned an example when he was talking about IP/MAC addresses:

Question: You just arrived in this class, and everyone in the class is a new student. How do you find a student named Xiao Ming in the class?
Answer: Call out Xiao Ming's name in the class. This works, and Xiao Ming will respond to you. The same goes for the network. However, if there is a seating chart with the seating information of each student, it would be much easier to find someone.

Environment variables are similar and are suitable for this example.

When a user executes a command in the command prompt, the command interpreter will look for the command to be executed.
So where does it look for executable commands? Two places:

  • Executable files in the current directory
  • Executable files in the paths saved in the environment variable Path (including system variables and user variables)

An example#

Let's take an example:

We all use win + r, which is often used to quickly run certain programs, such as opening the command prompt:

Open cmd

How does the computer know where cmd is?

Search cmd.exe in Everything

We can see that cmd.exe is in both C:\Windows\System32\ and C:\Windows\SysWOW64\. This means that the computer opens cmd.exe in one of these two paths. Now let's see if the system environment variable Path contains one of these two paths:

Check if the Path in the system environment variables contains this path:
Enter path in Cortana's search box to directly jump to the place to modify environment variables. If that doesn't work, you can only right-click on the computer icon and select Properties.

My environment variables

We can see that it does, which confirms our idea~ So we have also figured out this process:

You enter cmd in the Run window:
    -> The interpreter looks for this file:
        -> First, it checks if it exists in the current directory
        -> Then, it checks if it exists in the paths saved in the environment variable Path
    -> If it is not found, it reports that it is not found

So if you haven't configured an executable file to the Path, you have to manually enter the absolute path of the file to open it.

Other fields outside of Path#

Other fields are also convenient for us to use. When you want to use them, you can use %field_name% to call them.
For example, I set the CMDER_ROOT field in the system settings and assign it the value D:\0ArtinD\cmder, which is a path.
Set CMDER_ROOT

Then, if I want to open this path, I can use the field name:
Snipaste_2019-05-02_22-47-37

In simple terms, it's like a variable name in programming. Define a constant that can be used when needed.

In Windows CMD, we can use %VAR% to use a variable.

set VAR=hello
echo %VAR%

In Unix Bash (Linux, Mac, etc.), we can use $VAR to use a variable.

export VAR=hello
echo $VAR

In PowerShell, we can use $env:VAR to use a variable.

$env:VAR = "hello"
Write-Output $env:VAR

User variables and system variables#

The operating system has the concept of users.

User variables only take effect for the currently logged-in user.
System variables take effect for all users on the current computer.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.