[Linux] 8. Environment variables

1. Introduction of environment variables

First describe a phenomenon, when we execute a binary executable program, we need to find its location (the program must first be loaded into the memory to run, because the von Neumann architecture stipulates that the CPU can only read data from the memory ), so this is the reason why we bring ./ before running, so that the operating system can find and execute the corresponding command, then at this time, we are curious, why the command is not carried before the execution. /Woolen cloth?
Answer: The operating system needs to make preparations in advance (the default installation path of the software in the configuration file in the operating system will be imported into the memory), and a memory-level variable (environment variable PATH) will be constructed when the operating system starts the
shell , import PATH into the context of the shell, and when the command is executed, use the path in PATH to find the location corresponding to the command. The above
insert image description here
insert image description here
PATH is only to solve an application scenario. How to find the path of the command?
There are still many problems in the operating system?
For example, who is the user currently logging in to the system, who is the host name, how many historical commands are there, and what is the color scheme of the display?
Therefore, for this series of application scenarios, it is necessary to require the operating system to pre-set a batch of variables (global variables) that may be used in the future when starting the bash command line interpreter. These variables are called environment variables.

Understanding environment variables from an interpreted language:
Variables can be defined in C language/C++, and variables can also be defined on the command line.
And the shell is also a process, malloc or new can apply for space for us during the running process, which means that the process can adjust the space during running (as long as there is space, data can be saved, so the environment variable is essentially A piece of data: "string"), so the import of environment variables is defined in the process or loaded by malloc/new

2. The concept of environment variables

Environment variables (environment variables) generally refer to some parameters used in the operating system to specify the operating environment of the operating system
(for example: when we write C/C++ code, when we link, we never know the dynamics of our link. Where is the static library, but it can still be linked successfully to generate an executable program, because there are related environment variables to help the compiler find it.) Environment variables
usually have some special purposes and
usually have global characteristics in the system

3. Common environment variables

3.1 PATH

Specify the search path for the command (see the introduction above for details)

3.2 USER

insert image description here
insert image description here

3.3 PWD

insert image description here

3.4 Some other environment variables

insert image description here
The significance of the existence of environment variables: In different application scenarios, users can access environment variables to achieve their goals (convenient for users to use)

4. Commands related to environment variables

4.1 echo: display an environment variable value

4.2 export: set a new environment variable

4.3 env: Display all environment variables

insert image description here
First of all, bash is a system process, and after ./mycmd runs the program, the program becomes a process, and the process is a sub-process of bash (matchmaker: Wang Po recruits interns, creates sub-processes to execute commands) From the above results, it can be
concluded The following conclusion:
Environment variables have global attributes because they will be inherited by child processes (environment variables are originally defined for bash to use).
As for why child processes should inherit them?
–Because there are different application scenarios (for example: let bash find the instruction path PATH, user identity identification USER) need to be completed by sub-processes (also need to obtain environment variables)
So at this time, we can also understand the globality of environment variables and the locality of local variables (only valid under the current process bash)
When myval is not imported globally, only bash can be used, and after being imported, its child processes can also be used

4.4 set: Display locally defined shell variables and environment variables

4.5 unset: Clear environment variables

insert image description here

5. How to get environment variables through code

5.1 The concept of command line parameters

program name + options == command line arguments
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

Summarize

insert image description here

ask questions

insert image description here

Guess you like

Origin blog.csdn.net/weixin_60915103/article/details/130547442